def sieve(n):
    return list(primes(n))


def primes(n):
    if n < 2:
        raise StopIteration
    yield 2
    not_prime = set()
    for i in range(3, n+1, 2):
        if i not in not_prime:
            yield i
        not_prime.update(range(i*i, n, i))

def sieve(n):
    if n < 2:
        return []
    not_prime = set()
    prime = [2]
    for i in range(3, n+1, 2):
        if i not in not_prime:
            prime.append(i)
            not_prime.update(range(i*i, n, i))
    return prime

Switch to more optimal non-generator solution
Switch to more optimal non-generator solution
