def_odd_iter (): N= 1 while(True): N= n + 2yieldNdef_not_divisable (n):return LambdaX:x% n >0defprimes ():yield2it=_odd_iter () while(True): N=Next (IT)yieldN It=Filter (_not_divisable (n), it) forNinchprimes ():ifN < 1000: Print(n)Else: Break
First, list 2
all the natural numbers from the beginning, constructing a sequence:
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...
To take the first number of a sequence 2
, it must be a prime, and then 2
sift away the multiples of the sequence 2
:
3, 4, 5, 6, 7, 8, 9, ten, one, A, a, 16, +, +, ...
To take the first number of a new sequence 3
, it must be a prime, and then 3
sift away the multiples of the sequence 3
:
5, 6, 7, 8, 9, Ten, one, A, a, A, A, >18, ...
Take the first number of a new sequence 5
, and then sift away the multiples of the 5
sequence 5
:
7, 8, 9, Ten, one, A, and a , 0>18, ...
Keep sifting down and you can get all the primes.
Python-type Sieve method for prime numbers