让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N
(<105),请计算不超过N
的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N
。
输出格式:
在一行中输出不超过N
的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
代码长度限制
16 KB
时间限制
200 ms
内存限制
64 MB
#include <stdio.h>
#include <math.h>
int main(){
int n,i,j,flag=0,cot=0,temp=2;//2是最小素数
scanf("%d",&n);
for(i=3;i<=n;i++){
for(j=2;j<=sqrt(i);j++){//素数判断
if(i%j==0){
flag=1;
break;
}
}
if(flag==0){
if(i-temp==2)
cot++;//素数对计数
temp=i;//相邻素数的重置
}
flag=0;
}
printf("%d",cot);
return 0;
}