Multiple methods are used to determine the prime number and determine the prime number.
Prime Number definition:
It refers to the number of integers in a natural number greater than 1 that cannot be divisible by other natural numbers except 1 and itself. In other words, only the natural numbers of two positive factors (1 and itself) are prime numbers.
I will provide several methods to determine the prime number of a natural number n, mainly considering the code execution efficiency.
First, according to the definition of prime numbers, one method that everyone will think of is to traverse 2 ~ N-1. If n can be divisible by the number, n is not a prime number; otherwise, n is a prime number.
Code:
1 // method 1 (traversal) 2 int prime_1 (int n) 3 {4 for (int I = 2; I <n; I ++) 5 {6 if (n % I = 0) 7 return 0; 8} 9 return 1; 10}
The maximum number of cycles in method 1 is: N-2, although it can solve the problem, but this is not a good method. If the n value is large, you can use another method, that is, to open the n root number, just traverse 2 ~ Root number n.
Code:
1 // method 2 (for n) 2 int root (int n) 3 {4 return (int) sqrt (float) n ); 5} 6 int prime_2 (int n) 7 {8 for (int I = 2; I <= root (n); I ++) 9 {10 if (n % I = 0) 11 return 0; 12} 13 return 1; 14}
You may find that in a for loop, although the number of cycles is reduced, you must call the root (int n) function for each loop, we know that the calculation of the root number is very time-consuming in the computer. If every cycle needs to be calculated once, the program's efficiency will certainly not be high, since the value of the root number of n remains unchanged during the program running, you can use a variable to save the value. You do not need to calculate it for each loop. You only need to take the value of the variable.
Code:
1 // method 3 (only one square open) 2 int prime_3 (int n) 3 {4 int bound = root (n); 5 for (int I = 2; I <bound; I ++) 6 {7 if (n % I = 0) 8 return 0; 9} 10 return 1; 11}
We can further consider that the prime number will certainly not be an even number (because all even numbers can be divisible by 2, so all even numbers are not prime numbers), and both 2, 3, and 5 are prime numbers, A number greater than 2, 3, and 5 is not a prime number if it can be divisible by 2, 3, or 5.
Based on this idea, we have 4th methods to determine prime numbers.
Code:
1 // Method 4 (excluding the number of divisible by 2, 3, 5 and an even number) 2 int prime_4 (int n) 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); 10 11 int bound = root (n); 12 for (int I = 7; I <= bound; I ++ = 2) // The even number is not a prime number and does not traverse 13 {14 if (n % I = 0) 15 return 0; 16} 17 return 1; 18}
At present, our code execution efficiency is relatively high. Compared with the 1st methods, the number of loop executions is reduced a lot, but there is another factor that affects the execution efficiency of the program, that is, it takes time to calculate the n root number. Therefore, we need to eliminate the calculation of the root number. We can convert the calculation of the root number to the multiplication calculation, and the multiplication calculation is faster than the calculation of the root number. For the specific implementation process, see the following code.
Code:
1 // Method 5 (multiplication replaces the square) 2 for (int I = 7; I * I <= n; I + = 2) 3 {4 if (n % I = 0) 5 return 0; 6} 7 return 1;
Conclusion: In the above 5, the method for determining the prime number is based on the execution efficiency of the program, which affects the execution efficiency of a program, including the number of lines executed by the Code and the length of time consumed by each line of code,
Therefore, optimization such as reducing the number of cycles and replacing the root number calculation with multiplication improves the execution efficiency.
Appendix: The above code references the book "programming Pearl". I learned these optimization methods from the book and provided them for everyone to learn and share.