当前位置:首页 / 文章测试 / 判定素数的三种写法

判定素数的三种写法

开始打字练习

bool isprime(int x)

{

if(x < 2) return 0;

for(int i = 2; i <= x / i; i++)

if(x % i == 0)

return 0;

return 1;

}

#include<iostream>

#include<cmath>

#include<cstring>

using namespace std;

int n, cnt = 0;

bool a[100000005];

void sf(int x)

{

memset(a, 1, sizeof a);

a[0] = a[1] = 0;

for(int i = 2; i <= x; i++)

{

if(a[i] == 1)

{

cnt++;

for(int j = i + i; j <= x; j += i)

{

a[j] = 0;

}

}

}

}

int main()

{

cin >> n;

sf(n);

cout << cnt << "\n";

return 0;

}

#include<iostream>

#include<cmath>

#include<cstring>

using namespace std;

int n, cnt = 0;

bool a[100000005];

void sf(int x)

{

memset(a, 1, sizeof a);

a[0] = a[1] = 0;

for(int i = 2; i <= x; i++)

{

if(a[i] == 1)

{

cnt++;

for(int j = i + i; j <= x; j += i)

{

a[j] = 0;

}

}

}

}

int main()

{

cin >> n;

sf(n);

cout << cnt << "\n";

return 0;

}

声明:以上文章均为用户自行发布,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。