Skip to content

Commit

Permalink
Optimized Sieve of Eratosthenes
Browse files Browse the repository at this point in the history
Optimized Prime Generator in Python using psyco module
  • Loading branch information
detel committed Jul 9, 2015
1 parent c62f4aa commit 03edca4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Prime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from math import sqrt

try:
import psyco
psyco.full()
except:
pass

primes = []
#fp = open("/home/deepit/Desktop/hmm",'w')

def get_primes(n):

sieve = [True] * (n/2)

for i in xrange(3,int(n**0.5)+1,2):
if sieve[i/2]:
sieve[i*i/2::i] = [False] * ((n-i*i-1)/(2*i)+1)

return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]


primes = get_primes(100000000)
print primes
#qq = fp.write(str(primes))
#fp.close()

0 comments on commit 03edca4

Please sign in to comment.