ACM Number Theory-Prime Numbers

ACM Number Theory——Prime Numbers 


 

Definition of prime numbers :

        There are infinitely many prime numbers . A prime number is defined as a natural number greater than 1 that has no other factors other than 1 and itself . Such numbers are called prime numbers. Examples: 2, 3, 5, 7, 11, 13, 17, 19. (At that time there was also a saying called "prime number", but in terms of language, I think the term "prime number" is more compatible with "composite number". In analogy to "chemical element" and "compound", it is called "" prime numbers" is very apt)

Some properties of prime numbers:

  1. There are only two divisors of a prime number p: 1 and p;
  2. Any natural number greater than 1 is either itself a prime number or can be decomposed into the product of several prime numbers, and this decomposition is unique;
  3. An even number can be written as the sum of two composite numbers, each of which has at most 9 prime factors;
  4. An even number must be written as a prime number plus a composite number, where the number of factors of the composite number has an upper bound;

Prime Number Application:

  1.  Mathematically, prime numbers have many unproven properties; practically speaking, public key cryptography is a good example.
  2. Prime numbers are to number theory what elements are to chemistry. (Excerpted from Zhihu)

 Judgment prime number:

1  // Determine whether it is a prime number 
2  int IsPrime( int x)
 3  {
 4      if (x<= 1 )     // 0,1, negative numbers are all non-prime numbers 
5          return  0 ;
 6      int ans=( int )sqrt(x) + 1 ;      /* Calculate the upper bound of enumeration, in order to prevent the loss of precision caused by the ans value, so use the square root value to round up and add 1, that is, I would rather enumerate one more number than one less number */ 
7      for ( int i= 2 ; i<ans; i++ )
 8      {
 9          if (x%i== 0 )
 10          {
11             return 0;
12         }
13      }
14     return 1;
15 }
View Code

 Prime number sieve:

  1. Open a large bool-type array prime[], the size is n+1. First, mark all the odd numbers as true, and mark the even numbers as false.

  2. The code is as follows:

for( i=3; i<=sqrt(n); i+=2 )
{  
        if(prime[i])
          for( j=i+i; j<=n; j+=i )
                prime[j]=false;
 }

    3. Finally, the subscript of the unit whose value is true in the output bool array is the prime number within n.
      The principle is very simple, that is, when i is a prime (prime) number, all multiples of i must be composite numbers. If i has been judged not to be a prime number, then find the prime number after i to sieve out the multiples of this prime number. 
    A simple process of sieving primes: n=30.

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


    After step 1, the 15 units 4 ... 28 30 are marked as false, and the rest are true.
    Step 2 starts:
     i=3; since prime[3]=true, put prime[6], [9], [12], [15], [18], [21], [24], [27] , [30] is marked as false.
     i=4; Since prime[4]=false, the sieving step is not continued.
     i=5; Since prime[5]=true, mark prime[10],[15],[20],[25],[30] as false.
     i=6>sqrt(30) algorithm ends.
    The third step is to output the subscript whose prime[] value is true:
     for(i=2; i<=30; i++)
       if(prime[i]) printf("%d ",i);
    the result is 2 3 5 7 11 13 17 19 23 29

  The following figure shows the prime number sieve with n=120:

// 1: This is the most primitive prime number sieve method 
#define Max 1000000
 bool prime[Max];
 void IsPrime(){
     prime[0]=prime[1]=0;prime[2]=1;
     for(int i=3;i<max;i++)
        prime[i]=i%2==0?0:1;
     int t=(int)sqrt(Max*1.0);
     for(int i=3;i<=t;i++)
       if(prime[i])
         for(int j=i;j<Max;j+=i)
            prime[j] = 0 ;
}
// 2: The optimized sieve method, it can be found by manually simulating the original sieve method that a certain number may be deleted more than once
 //    The optimized sieve method can avoid this unnecessary deletion operation 
#define Max 1000000
 bool prime[Max];
 void IsPrime(){
     prime[0]=prime[1]=0;prime[2]=1;
     for(int i=3;i<max;i++)
        prime[i]=i%2==0?0:1;
     int t=(int)sqrt(Max*1.0);
     for(int i=3;i<=t;i++)
       if(prime[i])
         for(int j=i*i;j<Max;j+=2*i)//优化 
            prime[j]=0;
}
View Code

 Fast Linear Sieve Method  :

  The above method is easy to understand. Initially, it is assumed that all are prime numbers. When a prime number is found, it is obvious that the prime number is a composite number after multiplying it by another number.

  Filter out these composite numbers, which is the origin of the algorithm name. However, after careful analysis, it can be found that this method will cause repeated screening of composite numbers and affect the efficiency.

  For example, 10, when i=2, k=2*15 is sieved once; when i=5, k=5*6, it is sieved again. Therefore, there is a fast linear sieve method.

  Every composite number must have a minimum prime factor. Every composite number is sieved by its smallest prime factor exactly once. So it is linear time.

void get_prime()
{
    int cnt = 0;
    for (int i = 2; i < N; i++)
    {
        if (!tag[i])    p[cnt++] = i;
        for (int j = 0; j < cnt && p[j] * i < N; j++)
        {
            tag[i*p[j]] = 1;
            if (i % p[j] == 0)
                break;
        }
    }
}
function template
1  I recommend this algorithm! It is easy to understand, only the odd part is counted, and the time and space efficiency is not bad!
2 half=SIZE/ 2 ; 
 3  int sn = ( int ) sqrt(SIZE); 
 4  for (i = 0 ; i < half; i++ ) 
 5     p[i] = true ; // Initialize all odd numbers to be prime. p[0] corresponds to 3, that is, p[i] corresponds to 2*i+3 
6  for (i = 0 ; i < sn; i++ ) {    
 7  if (p[i]) // if i+i+3 is a prime number 
8  {     
 9      for (k=i+i+ 3 , j=k*i+k+i; j < half; j+= k) 
 10      //The starting point of the sieve method is the square of the prime number corresponding to p[i] k^2                                        
 11      // The position of k^2 in p is k*i+k+i
 12      //     The subscript ik*i+k+i
 13      // Corresponding value k=i+i+3 k^2          
14         p[j]= false ; 
 15  } 
 16  } 
 17  // The prime numbers are all stored in the p array, p[i]=true means that i+i+2 is a prime number.
18  // For example, 3 is a prime number, filter in the order of 3*3, 3*5, 3*7..., because only odd numbers are stored, so do not delete 3*4, 3*6....
recommend

 


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325338059&siteId=291194637