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:

  1. mark number selected prime.

  2. remove multiples of number set prevent them being selected in future.

  3. see 1)


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -