Enumeration 1 -- calculate the maximum prime number less than n, enumeration 1 -- prime number
Enumeration 1 -- calculate the maximum prime number less than n
1/* 2 enumeration is a problem solving strategy based on the answers of existing knowledge images. 3 4 problem: finding the maximum prime number smaller than n 5 6 Analysis: 7 cannot find a mathematical formula, so that according to N can calculate this prime number 8 9 we think: 10 N-1 is a prime number? Is N-2 a prime number ?... 11 12 so we are to determine whether the N-K is a prime number: 13 N-K is a prime number of sufficient conditions: N-K cannot be [2, n-k) in any division 14 15 to determine whether the N-K is a prime number can be converted: 16 calculate all prime numbers smaller than the N-K (the condition in "The maximum prime number less than N" is that "n cannot be divisible by any prime number in [2, n)", rather than an integer) 17 The number that cannot be divisible by any prime number in [2, n) must be a prime number, because all integers are base on prime numbers, so 18 does not need to be checked for All integers, check all prime numbers. 19 20. Solution: 21 2 is a prime number, which is recorded as PRIM 0 22. According to PRIM 0, PRIM 1 ,... prim k, find the smallest prime number prim k + 1 (here is based on the prime number to find the prime number) 23 if prim k + 1 is greater than N, then, prim k is the prime number we need to find. Otherwise, we will continue to look for 24 25 enumeration: 26 listing each element one by one from the possible set 27. Based on the knowledge we know, give a guess. Case 28: for example, 2 is a prime number, and 2 is the solution of this problem. 29 30 enumeration algorithm: 31 each of the sets of possible solutions for the problem: 32. Based on the test conditions given by the problem, determine which conditions are established. 33 makes the condition true, that is, the solution of the problem. 34 35 enumeration process: 36. Is the guess correct? 37 2 is the maximum prime number smaller than N? 38. Make a new guess: 39 there are two key factors to note: 40. 1. The result of the guess must have not been found in the previous guess. The prime number of each guess must be 41 larger than the prime number that has been found. 2. The incorrect answer should be ruled out early in the guess process. For example, except for 2, only an odd number can be a prime number 42 43 During enumeration: 44 1. what is the possible situation for establishing a brief mathematical model 45? 46. The number of variables in the model is as small as possible, and they are independent of each other. 47. The condition in "The maximum prime number less than N" is that "n cannot be [2, n) any prime number in '48 rather than 'n' cannot be divisible by any integer in [2, n) "49 2. reduce the search space 50 use knowledge to narrow the value range of each variable in the model to avoid unnecessary computation 51. For example: a small number of times executed by the loop body in the Code 52, except 2, only an odd number can be a prime number. {2, 2 * I + 1 | 1 <= I, 2 * I + 1 <n} 53 3. use the appropriate search order 54 The traversal order of the search space must be consistent with the conditional expression in the model 55 For example: For {2, 2 * I + 1 | 1 <= I, 2 * I + 1 <n}, 56 57 58 enumeration keywords (enumeration core) in ascending order ): 59 scale down 60 61 */62 63 # include <iostream> 64 using namespace std; 65 int prim [50000]; // used to store all prime numbers 66 int primNum = 0; // records the number of prime numbers already stored in the prim array 67 int times = 0; // records the total number of judgments for solving the problem 68 int primLessN (int n ); 69 int primLessN_2 (int n); 70 bool isPrimMothed (int n); // judge whether a number is a prime number 71 72/* 73 Method 1: the enumeration method judged by prime numbers after going: 74. The condition in "The maximum prime number less than N" is that "n cannot be divisible by any prime number in [2, n ", instead of an integer 75 76 When n = 10 0000, 77 ans = 99991 78 times = 4626 4478 times 79 primNum = 9592 80 81 each of my prime numbers is determined, we have to traverse the previous prime number Table 82, and when we judge 10 0000, the outer loop goes through 50000, each prime number in the layer is a traversal of the previous prime number table 83 50000*(1 + 2 + 3 +... + 9592) = 50000*4600 8082 84 The number above does not have 50000, but also minus those non-prime 85. From 50000*4600 8082, we can see that it is mainly the time spent by those prime numbers, non-prime number almost never takes time 86 non-prime number = 4626 4478-4600 8082 = 25 6450 87 only 0.25 million, although it is much more than below, because it is the 88 */89 int primLessN (int n) 90 {91 prim [0] = 2; // 2 is the smallest prime number 92 primNum ++; 93 for (int I = 3; I <n; I + = 2) {94 bool isPrim = 1; // isPrim is used to determine whether a number is a prime number 95 for (int j = 0; j <primNum; j ++) {96 times ++; 97 if (I % prim [j] = 0) {98 isPrim = 0; 99 break; // when n = 10 0000, times = 2 5239 6936 times (0.25 billion), followed by times = 4626 4478 4.5 times (100 million times) 101} 102 103} if (isPrim) prim [primNum ++] = I; // if it is a prime number, it is saved to the prim prime number array 104} 105 return prim [primNum-1]; 106} 107 108/* 109 method two: the integer enumeration method from the back is 110, and the space consumption of method 2 is also less than 111. 112 When n = 10 0000, 113 ans = 99991114 times = 346 times 115 116 When n = 100 0000, if method 1 is used, 117 ans = 99 9983118 times = 1811 times 119 120 when n = 1 0000 0000 (0.1 billion, 121 ans = 9999 9989122 times = 11314 times 123 124 when n = 10 0000 0000 (billion, 125 ans = 9 9999 9937126 times = 52537 times 127 */128 bool isPrimMothed (int n) {129 bool isPrim = 1; // isPrim is used to determine whether a number is a prime number. 130 if (n = 2 | n = 3) return 1; 131 for (int I = 2; I * I <= n; I ++) {132 times ++; 133 if (n % I = 0) return 0; 134} 135 return 1; 136} 137 138 int primLessN_2 (int n) {139 for (int I = n; I >=2; I --) {140 if (isPrimMothed (I) return I; 141} 142} 143 int main () {144 int n; 145 scanf ("% d", & n); 146 // int ans = primLessN (n ); 147 int ans = primLessN_2 (n); 148 cout <ans <endl; 149 printf ("total times of determination times: % d \ n", times ); 150 printf ("Total prime number primNum: % d \ n", primNum); 151 return 0; 152}
The code running result is included in the comment.