Skip to content

Instantly share code, notes, and snippets.

@Ayrx

Ayrx/prime_sieve.py

Created Jun 25, 2013
Embed
What would you like to do?
Sieve of Eratosthenes
def gen_prime_sieve(n):
# Implementation uses the Sieve of Eratosthenes
# Optimized code from: http://stackoverflow.com/a/3941967/1170681
a = [True] * n
a[0] = a[1] = False
for (i, isprime) in enumerate(a):
if isprime:
yield i
for n in xrange(i*i, n, i):
a[n] = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment