Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Created August 19, 2012 01:32
Show Gist options
  • Select an option

  • Save theonewolf/3390820 to your computer and use it in GitHub Desktop.

Select an option

Save theonewolf/3390820 to your computer and use it in GitHub Desktop.
Sieve of Erastosthenes
def sieve_of_erastosthenes(till):
primelist = [True]*(till+1)
primelist[0] = primelist[1] = False
nextprime = (i for i,v in enumerate(primelist) if v)
while (True):
try:
prime = nextprime.next()
except StopIteration:
return primelist
for i in xrange(prime*prime,till+1,prime):
primelist[i] = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment