Evaluate the prime number using the 'distinct' Method
I. Method for prime number calculation:The factor of one number N does not exceed SQRT (n ).
The Code is as follows: C/C ++ code
# Include <stdio. h> # include <math. h> voidprime (int n) {int I = 2; while (I <= SQRT (N) if (N % I ++ = 0) return; printf ("% d", n);} intmain () {int I, m, n; scanf ("% d", & M, & N ); for (I = m; I <= N; I ++) prime (I); Return 0 ;}
This method is only applicable to N hours, which is too time-consuming when n is large.
2. The method for filtering prime numbers is not to directly judge whether a number is a prime number, but to remove all the numbers that are not prime numbers, and the remainder is a prime number.
1. If the interval contains 1, first mark 1 as a non-prime number.
2. Starting from the next smallest prime number A, multiply the prime number (2a, 3A ,......, All are marked as non-prime numbers.
3. Find the next smallest prime number from the end of a and repeat the 2 operation.
4. Repeat operations 2 and 3 until all elements are filtered out.
For example, filter the prime numbers between 1 and 25.
① Perform step by step according to the above four steps. The first step is to mark 1 as a non-prime number. The second step is to find the next Prime Number A = 2, marking the multiples of 2, 6, 8 ,......, ; Step 3, repeat Step 2, when a = 3, mark 6, 9 ,......, ; Step 4, repeat Step 3 of step 2, A = 5, a = 7, A = 11, A = 13, A = 17, A = 19, A = 23
Steps are as follows:
The Code is as follows:C/C ++ code
# Include <stdio. h> # include <iostream> # include <string. h> using namespace STD; # define n 8099999 bool prime [N] ;__ int64 num [N], count; void getprime () {_ int64 I, J; memset (Prime, 0, sizeof (PRIME); Count = 0; for (I = 2; I <= N; I ++) {If (prime [I] = 0) {num [count ++] = I ;}for (j = 2 * I; j <= N; j + = I) if (prime [J]! = 1) prime [J] = 1 ;}} int main () {_ int64 I; getprime (); for (I = 0; I <count; I ++) printf ("% i64d", num [I]); Return 0 ;}
② When we look at the above steps again, we find that some non-prime numbers are marked more than once, for example, the number 6, when a = 2 and 3 are marked, if we only mark it once, it will save a certain amount of time, so that we can optimize the program. At the same time, we can also find that when a = 2, we only need to mark the multiples of 2 from 2x2 = 4; When a = 3, the above is marked from 6, in fact, we only need to mark the multiples of 3 from 3x3 = 9 (because 6 is already marked when a = 2); When a = 5, the above is marked from 10. In fact, we only need to mark the multiples of 5 from 5x5 = 25 (because 10 and 20 have been marked at a = 2, 15 has been marked at a = 3 ).
The process is shown in:
Based on the preceding method, we can use the following code:
# Include <stdio. h> # include <string. h> // n indicates the upper limit of the range [0, N] required # define n 5000000 // array Prime is used to mark non-prime bool prime [N]; // array p Stores prime numbers, count records the number of prime numbers _ int64 P [N], Count = 0; // The Void getprime () {_ int64 I, j; memset (prime, 0, sizeof (PRIME); for (I = 2; I <= N; I ++) {If (prime [I] = 0) {P [count ++] = I; for (j = I * I; j <= N; j + = I) prime [J] = 1 ;}}} int main () {getprime (); // all prime numbers between the output range [0, N] For (INT I = 2; I <count; I ++) printf ("% i64d", P [I]); Return 0 ;}
The core code is as follows:
// N indicates the upper limit of the range [0, N] required # define n 5000000 // array Prime is used to mark non-prime bool prime [N]; // array p Stores prime numbers, count records the number of prime numbers _ int64 P [N], Count = 0; // The Void getprime () {_ int64 I, j; memset (prime, 0, sizeof (PRIME); for (I = 2; I <= N; I ++) {If (prime [I] = 0) {P [count ++] = I; for (j = I * I; j <= N; j + = I) prime [J] = 1 ;}}}