#include <iostream>
#include <cmath>
#include <bitset>
using namespace std;
int main ()
{
int const max_number(1000000);
int const max_test((int)sqrt((double)max_number)); // sqrt 是标准库函数cmath的头文件,sqrt是double类型的,
bitset<max_number> numbers; // 101个0,
numbers.set(); //101个1,
numbers[1] = 0;
for(int i(1); i != max_test; ++i)
{
if(numbers[i]){
//筛掉倍数,
for(int j(i*i); j < max_number; j += i) //j(i*i)是从i的平方开始,
{
numbers[j] = 0;
}
}
}
cout << "the number of primes less than " << max_number + 1
<< " is " << numbers.count() << ".\n\n";
for(int i(1); i != max_number + 1; ++i)
{
if(numbers[i])
cout << i << ", ";
}
cout << endl; //输出是1000000内的所有质数,
return 0;
}
bitset查找质数
最新推荐文章于 2021-01-11 22:17:54 发布