Double Break out of Nested Loops

primes, python, python-3.x

Solution

Put the loops in a function and use the `return` keyword:

def func(primes):
    for a in range(3, 500, 2):
        for b in range(2, int(a ** 0.5 + 0.5)):
            if a % b != 0:
                primes.append(a)
            if a % b == 0:
                [x for x in primes if x != a]
                return

primes = [2]
func(primes)

This tends to be a good thing when it makes the programmer write modularized code.

Problem

I've seen many different ways to break out of two nested loops at once, but what is the quickest and simplest for my code? ``` primes = [2] for a in range(3, 500, 2): for b in range(2, int(a ** 0.5 + 0.5)): if a % b != 0: primes.append(a) if a % b == 0: [x for x in primes if x != a] # double break ```

Original source

Related problems