您的位置:首页 > 编程语言 > C语言/C++

c++ 素数检测算法归纳

2017-10-25 09:35 225 查看
后续慢慢整理

#include <iostream>
#include <cmath>
using namespace std;
bool prime(int x)
{
int y;
for (y = 2; y <= sqrt(x); y++)   //当x=3,判断条件不满足,不执行for内部语句,跳转到return true
if (x%y == 0)
return false;
return true;
}
int main()
{
int n, i;
//cin >> n;
n = 100;
if (n >= 2)
cout << "2 ";
for (i = 3; i <= n; i++)
if (prime(i))
cout << i << " ";

while (1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言