Created
September 2, 2015 15:28
-
-
Save juliengrenier/7d6c9bf3bf161fb6add4 to your computer and use it in GitHub Desktop.
Prime generator
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 genprimes(upto): | |
primes = {} | |
q = 2 | |
i = 0 | |
while i < upto: | |
if q not in primes: | |
yield q | |
i += 1 | |
primes[q*q] = [q] | |
else: | |
for p in primes[q]: | |
primes.setdefault(p+q, []).append(p) | |
del primes[q] | |
q += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment