您的位置:首页 > 其它

PAT (Basic Level) Practice 1007 素数对猜想

2018-03-26 18:59 459 查看

1007. 素数对猜想 (20)

时间限制400 ms
内存限制65536 kB
代码长度限制8000 B
判题程序Standard作者CHEN, Yue
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。输入格式:每个测试输入包含1个测试用例,给出正整数N。输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。输入样例:
20
输出样例:
4

#include<cstdio>
#include<cmath>
using namespace std;

bool judge(int a){
for(int i = 2; i < sqrt(a) + 1; i++)
if(a % i == 0) return false;
return true;
}
int main(){
int n;
int cnt = 0;
scanf("%d", &n);
for(int i = 3; i + 2 <= n; i++){
if(judge(i) && judge(i+2)) cnt++;
}
printf("%d", cnt);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: