python - Understand fast prime numbers generator -
def sieve_for_primes_to(n): size = n//2 sieve = [1]*size limit = int(n**0.5) in range(1,limit): if sieve[i]: val = 2*i+1 tmp = ((size-1) - i)//val sieve[i+val::val] = [0]*tmp return sieve print [2] + [i*2+1 i, v in enumerate(sieve_for_primes_to(10000000)) if v , i>0]
can please describe how code works?
this called sieve of eratosthenes , wiki page job of describing it.
the gist of goes this:
you select numbers starting 2 , going up, you:
mark number selected prime.
remove multiples of number set prevent them being selected in future.
see 1)
Comments
Post a Comment