When a number is not big, you can use the ordinary method to calculate the prime number, but if a number is too large, it is like asking for the number of prime numbers between 1, the ordinary method won't work. This requires the prime number embedding method. Its time complexity is O (n). Although not very good, it is also a fast method so far, it exchanges space for time. Today's computers and spaces have some, but time is very precious. Efficiency issues are particularly important. Its principle is to mark and prevent repeated judgment, which improves the efficiency. Just like 2 is a prime number, and all the multiples of 2 are certainly not prime numbers. At this time, we mark it and judge that 3 is a prime number. All the multiples of 3 are certainly not prime numbers, in this case, we need to mark it and execute it to the root (total number). In this case, we will get a prime number table. All the unlabeled ones are prime numbers. below is the specific code implementation, the Code contains comments. Clearly written.
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cstring> 4 # define Max 1000000000 // define an array length of 5 bool prime [Max + 1]; // prime number table 6 int main () 7 {8 int I, j, Count = 0; // count is counter 9 memset (Prime, true, sizeof (PRIME )); // Initialize all prime arrays to true10 prime [0] = prime [1] = false; prime [2] = true; // none of them are prime numbers, therefore, it is false, and 2 is a prime number. Therefore, true11 for (I = 2; I * I <= max; I ++)/* traverses from 2, I * I <= max is equivalent to I <SQRT (max). However, the former is even less prone to errors. 12 it is not necessary to explain why it is SQRT, this */13 {14 if (prime [I]) is also available in common methods. // if it is not marked, mark the number of its multiples (marked as false) 15 {16 for (j = I * I; j <= max; J + = I) // because it starts from 2, j = I * I is enough. The smallest of them is I * I, it cannot be smaller than this one. 17 Prime [J] = false; // mark it as false18} 19} 20 for (I = 2; I <= max; I ++) // traverse and find all the prime numbers to 21 if (prime [I]) 22 count ++; // if not marked, the counter ++ 23 printf ("count is: % d \ n ", count); 24 return 0; 25}
At this time, the most basic, of course, can also be improved. The more improvements, the faster the execution efficiency. For example, because an even number cannot be a prime number, it must be divisible by 2, so we can discard all the even numbers, which will reduce the time by half, and the space will also be reduced by half. If it is billions, just open an array of 0.5 billion, the I in the for loop should start from 3, and then add 2 at a time, so that the mark will be faster and the specific code will not be implemented. The general principle is similar. This is a very basic algorithm. Prime numbers are also used in many cases. Of course, you can also focus on improvement. So far I have not thought of a better solution. If any of you have a better solution, please leave it blank. Thank you!
Prime Number Embedding Method