Created
June 25, 2013 12:46
-
-
Save Ayrx/5858181 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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