Last active
September 30, 2015 20:28
-
-
Save Deathnerd/a98500ed7c87f9c772d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 primes(): | |
""" | |
Generate an infinite series of primes based on the | |
Sieve of Eratosthenes | |
""" | |
D = {} | |
q = 2 | |
while True: | |
if q not in D: | |
yield q | |
D[q*q] = [q] # Thank you Eratosthenes | |
else: | |
for p in D[q]: | |
# Back-fill the dictionary | |
# With keys being composites and the | |
# list value being its components | |
D.setdefault(p + q, []).append(p) | |
del D[q] | |
q += 1 | |
primes_gen = primes() # instantiate the generator | |
print [next(primes_gen) for x in range(0,10)] # use it! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment