To get all the prime number of natural number N, it must be not greater than
The multiples of all prime numbers of are removed, and the rest are prime numbers.
Give it to sievevaluerange n, find out。
1 is neither quality nor number, remove;
Use 2 to sieve first, that is, leave 2, remove the multiple of the 2;
Use the nextquality, that is, 3 sieves, leave 3, remove the multiple of 3;
Next, use the next quality 5 sieve, leave 5, and 5multipleRemove; Repeat it constantly …
Mainly use BITSET type
1 #include<iostream> 2 #include<string> 3 #include<bitset> 4 #include<cmath> 5 6 using namespace std; 7 int main() 8 { 9 int const max_number(32); 10 int const max_test((int)sqrt((double)max_number)); 11 bitset<max_number + 1> number; 12 number.set(); 13 number[1] = 0; 14 for (int i(1); i!=max_test; ++i) 15 { 16 if (number[i]){ 17 //S screening multiple 18 for (int j(i*i); j <max_number + 1; j += i) 19 { 20 number[j] = 0; 21 } 22 } 23 } 24 cout << "The number of the primes less than " << max_number + 1 25 << "is" << number.count() << endl; 26 for (int i(1); i != max_number + 1; ++i) 27 { 28 if (number[i]) cout << i << ","; 29 } 30 cout << endl; 31 system("pause"); 32 }
Reprinted: https://www.cnblogs.com/dameidi/p/9313461.html