讓我們定義dn爲:dn=pn+1−pn,其中pi是第i個素數。顯然有d1=1,且對於n>1有dn是偶數。“素數對猜想”認爲“存在無窮多對相鄰且差爲2的素數”。
現給定任意正整數N
(<105),請計算不超過N
的滿足猜想的素數對的個數。
輸入格式:
輸入在一行給出正整數N
。
輸出格式:
在一行中輸出不超過N
的滿足猜想的素數對的個數。
輸入樣例:
20
輸出樣例:
4
C++示例代碼:
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int N;cin>>N;
int pn,pnn;//pnn=Pn+1
int count = 0;
for(int i=2;i<=N;i++) {
int m = sqrt(i);
int j;
for(j=2;j<=m;j++) {
if(i%j==0)
break;
}
if(j>m && i==2)
pn = i;
if(j>m) {
pnn = i;
if(pnn-pn==2)
count++;
pn = pnn;
}
}
cout<<count;
return 0;
}