Skip to content

Instantly share code, notes, and snippets.

@samrat
Created July 2, 2011 16:22
Show Gist options
  • Save samrat/1061069 to your computer and use it in GitHub Desktop.
Save samrat/1061069 to your computer and use it in GitHub Desktop.
Implemented with a bit of help from the Wikipedia page ;) Runs much faster than the naive approach.
def sieve(n):
"""Finds all prime numbers upto n
"""
primelist = []
intlist = list(range(int(n+1)))
fin = int(n**0.5)
for i in range(2,fin+1):
intlist[2*i::i] = [None] * len(intlist[2*i::i])
for item in intlist[2:]:
if item: primelist.append(item)
return primelist
#runtime calculation
import time
tStart=time.time()
sieve(6000)
print("Program complete")
print("Time taken:", time.time()-tStart)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment