Created
October 1, 2011 21:25
-
-
Save andlima/1256678 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes in Python
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(maximum): | |
discarded = set() | |
was_discarded = discarded.__contains__ | |
discard_all = discarded.update | |
limit = maximum + 1 | |
yield 2 | |
for m in xrange(3, limit, 2): | |
if not was_discarded(m): | |
yield m | |
discard_all(xrange(m * 2, limit, m)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, "web scale" this: https://gist.github.com/1256699 :c)