#include<iostream>
#include<ctime>
using namespace std;
const int MAX = 3000;
char tables[MAX];
void cal_prime()//筛选法求素数,加法的效率要高于除法
{
memset(tables, 1, MAX);
for (int i = 2; i != MAX; ++i)
{
if (tables[i])
{
for (int j = i + i; j < MAX; j += i)//一个数字的倍数必然不会是素数
tables[j] = 0;
}
}
}
int main()
{
//clock_t now = clock();
cal_prime();
//cout << "execution time:" << clock() - now << endl;
int count(8);//制表
for (int i = 2,j = 0; i != MAX; ++i)
{
if (tables[i])
{
cout << i;
(j++ % count == 7) ? cout << endl : cout << ' ';//每行输出8个数字
}
}
system("pause");
return 0;
}
筛选法求素数
最新推荐文章于 2021-05-19 20:19:11 发布