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 -

reflection - How to access the object-members of an object declaration in kotlin -

php - Doctrine Query Builder Error on Join: [Syntax Error] line 0, col 87: Error: Expected Literal, got 'JOIN' -