Eratosthenes sieve

slate slate is offline Offline Jun 7th, 2008, 7:57 am |
0
The most efficient static sieve generator in python I have seen so far.
Returns the list of primes, that are not greater than n.
Quick reply to this message  
Python Syntax
  1. def eras(n):
  2. siv=range(n+1)
  3. siv[1]=0
  4. sqn=int(round(n**0.5))
  5. for i in range(2,sqn+1):
  6. if siv[i]!=0:
  7. siv[2*i:n/i*i+1:i]=[0]*(n/i-1)
  8. return filter(None,siv)

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC