让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N
(<105),请计算不超过N
的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N
。
输出格式:
在一行中输出不超过N
的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
我们可以发现,3和5是一组,5和7是一组········此外还需判定两数是不是素数,所以只要在奇数里面找就可以了。
#include<bits/stdc++.h>
using namespace std;
bool pd(int n){
for(int i=3;i<=sqrt(n);i++){
if(n%i==0)return false;
}
return true;
}
int main(){
int n;
cin>>n;
if(n>=5){
if(n>=5&&n<=6)cout<<"1";
else if(n>6&&n<9)cout<<"2";
else if(n>8&&n<=13)cout<<"3";
else {
int sum=3,q=15,h=17;
while(h<=n){
if(pd(q)&&pd(h))sum++;
q+=2;h+=2;
}
cout<<sum;
}
}
else cout<<"0";
return 0;
}