#include <bitset> //埃拉托斯特尼——筛法
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int const max_number(100);
int const max_test((int)sqrt((double)max_number));
bitset<max_number+1> numbers; //101个0!
numbers.set(); //101个1!
//numbers[0] //不理他
numbers[1] = 0;
for(int i(1);i != max_test;i++)
{
if(numbers[i]){
//筛掉倍数,
for(int j=i*(2)i;j<max_number+1;j+=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;
return 0;
}
转载于:https://blog.51cto.com/canyueweizhui/1699146