prime number, which means that in a natural number greater than 1, except for 1 and the integer itself, it can not be divided by other natural numbers. It plays an important role in encryption applications. For example, the widely known RSA algorithm is based on factoring problem of large integers, finding two super prime numbers and multiplying them as keys. A more common for prime solution is Ella Torsten Ni (the Sieve of Eratosthenes sieve) , saying simply is the painting form, and then delete the table, as shown in figure


" began to turn back from number 2, if the current number is a prime number, then all its multiples of the number of removed from the table or the marker, and then finally get all the prime numbers.

has an optimization: when the

marks the multiple of 2 and 3, 6 is marked two times. So the markup from the square of the I reduces a lot of time.

, such as the multiplier of 3, is marked from 9, not 6, and 6 at a time.

, except 2, is an odd number of prime numbers. The odd number of square is still odd, and if the odd number is added, the even number will not be a prime, so add the even number (2 times the prime).

prehandles all even numbers. Note:

1 is not a prime number is not, there is no 1.

 #!  import time    def primes(n):   P = []   f = []   for i in range(n+1):    if i > 2 and i%2 == 0:     f.append(1)    else:     f.append(0)     i = 3   while i*i <= n:    if f[i] == 0:     j = i*i     while j <= n:      f[j] = 1      j += i+i    i += 2     P.append(2)   for i in range(3,n,2):    if f[i] == 0:     P.append(i)     return P    def isPrime(n):   if n > 2 and n%2 == 0:    return 0     i = 3   while i*i <= n:    if n%i == 0:     return 0    i += 2     return 1    def primeCnt(n):   cnt = 0   for i in range(2,n):    if isPrime(i):     cnt += 1   return cnt    if __name__ == '__main__':   start = time.clock()   n = 10000000   P = primes(n);   print("There are %d primes less than %d"%(len( P), n #for I in)) range (10): # print (P[i]) print ("Time:%f" (time.clock) -start (%) #for n) in range (2100000): # if isPrime (n): # print ("%d is prime"%n "%d is" (#print)%n + ("prime" if isPrime (n) else "not prime") start time.clock (n) = 1000000) = print ("There are%d primes less than%d" (% primeCnt (n), n)) print ("Time:%f" (time.clock -start)% (


with Prime numbers screening method for the primes less than 10 million with 5.767s,

ordinary number judgment method for the primes less than 1 million with 9.642s,

C++ prime screening method for the primes less than 100 million with 0.948s,

C++ with a general number judgment method for the primes less than 10 million with 3.965s

visible language indeed much slower than compiled languages.


 #include <iostream>  #include <cstdio>  #include <algorithm>  using namespace std;  #define N 100000001    unsigned f[(N>>5)+5];  int p[5761456],m;  void init()  {    int i,j;    for(i=4;i<N;i+=2)      f[i>>5]|=1<<(i&0x1F);    p[m++]=2;    for(i=3;i*i<N;i+=2)      if(!(f[i>>5]&(1<<(i&0x1F))))      {        p[m++]=i;        for(j=i*i;j<N;j+=i+i)          f[j>>5]|=1<<(j&0x1F);      }    for(;i<N;i+=2)      if(!(f[i>>5]&(1<<(i&0x1F))))        p[m++]=i;  }  int is_prime(int n)  {    int i;    for(i=0;p[i]*p[i]<=n;i++)      if(n%p[i]==0)        return 0;    return 1;  }  in T isPrime (int n) {if (n> 2; & & n%2==0) return 0; int i=3; while (i*i< =n) {if (n%i==0) return 0; i+=2 return 1;};} int {int (main) n=0, I; clock_t st=clock; init (()); /*for (i=2; i< 10000000; i++ (if) isPrime (I) printf (n++); / / "%d%dmsn", m, clock) (-st); /*while (~scanf (%d, & N, n)) {i=lower_bound (P, p+m, -p; printf (n+1) "%dn, I}*/); return 0;} 

all above is the

, want to help everyone to learn, I hope you will support a script.

you might be interested in this article:

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Analysis of Python prime number screening method | Script Home

You may also be interested in these articles!