Several methods to realize the judgment of primes
Definition of prime number:
The number of integers in a natural number greater than 1, except 1 and the integer itself, cannot be divisible by other natural numbers. In other words, the natural number of only two positive factors (1 and self) is prime.
I will give several methods to realize the prime number of natural numbers n, mainly from the execution efficiency of the code to consider this problem.
First, according to the definition of prime number, we all think of one way is to traverse the 2~n-1, if n can be divisible by the number, then n is not a prime, otherwise the prime.
Code:
1 //Method 1 (traversal)2 intPrime_1 (intN)3 {4 for(intI=2; i<n;i++)5 {6 if(n%i==0 )7 return 0;8 }9 return 1;Ten}
Method 1 The maximum number of cycles is: n-2, although the problem can be solved, but this is not a good way. If the n value is large, you can use another method, that is, to open the square root of N, simply traverse the 2~ square root N.
Code:
1 //Method 2 (open radical for n)2 intRootintN)3 {4 return(int) sqrt (float) n);5 }6 intPrime_2 (intN)7 {8 for(intI=2; I<=root (n); i++)9 {Ten if(n%i==0 ) One return 0; A } - return 1; -}
Careful you may find that, in the For loop, although the number of cycles will be reduced, but each loop to call the root (int n) function, we know that the open square is a computer is very time-consuming, if each cycle is calculated once, the efficiency of the program is certainly not high, Since the value of the open square root of n is constant during program operation, you can save this value with a variable, and each cycle does not need to be evaluated, just take the value of the variable.
Code:
1 //Method 3 (calculate only one square)2 intPrime_3 (intN)3 {4 intBound=root (n);5 for(intI=2; i<bound;i++)6 {7 if(n%i==0 )8 return 0;9 }Ten return 1; One}
We can further consider that the prime number is definitely not even (because all the even numbers are divisible by 2, so all the even numbers are not primes), and 2, 3, and 5 are primes, which are larger than 2, 3, and 5 if they are divisible by 2 or 3 or 5, then this number is not prime.
According to this idea, we have the 4th method of judging primes.
Code:
1 //Method 4 (excludes numbers that are divisible by 2, 3, 5, and even)2 intPrime_4 (intN)3 {4 if(n%2==0 )5 return(n==2);6 if(n%3==0 )7 return(n==3);8 if(n%5==0 )9 return(n==5);Ten One intBound=root (n); A for(intI=7; i<=bound;i+=2)//An even number is not a prime number and does not traverse - { - if(n%i==0 ) the return 0; - } - return 1; -}
Now our code execution efficiency is relatively high, compared to the 1th method, the number of cyclic execution decreased a lot, but there is also a very influential factor in program execution efficiency, that is, the calculation of n open square root, the open square root calculation is very time-consuming, so we need to eliminate the calculation of open square root. We can convert the open square root calculation to multiplication calculation, the multiplication calculation will be faster than the open square root calculation, the specific implementation process please see the following code.
Code:
1 // 2 for (int i=7 ; I*i<=n;i+=2 ) 3 { if (N%i==0 ) 5 return 0 ; 6 7 return 1 ;
Summary: The method of judging primes in the above 5 is based on the execution efficiency of the program, which affects the execution efficiency of a program with the number of code execution lines, and the length of time that code consumes each line of code.
Therefore, the reduction of the number of cycles and the substitution of the open-square-root calculation for the multiplication operation have improved the efficiency of the execution.
Attached: The code above refers to the "programming Zhu Ji Nanxiong" this book, from books to learn these optimization methods, to provide you with learning and sharing.
Several methods to realize the judgment of prime number