題目鏈接:https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744
題目描述
讓我們定義dn爲:dn=pn+1−pn,其中pi是第i個素數。顯然有d1=1,且對於n>1有dn是偶數。“素數對猜想”認爲“存在無窮多對相鄰且差爲2的素數”。
現給定任意正整數N(<105),請計算不超過N的滿足猜想的素數對的個數。
輸入
輸入在一行給出正整數N。
輸出
在一行中輸出不超過N的滿足猜想的素數對的個數。
樣例輸入
20
樣例輸出
4
代碼
#include <cstdio>
bool is_prime(int x) {
for(int i = 2; i * i <= x; i++) {
if(x % i == 0)
return false;
}
return true;
}
int main() {
int n, t = 0, a[100010];
scanf("%d", &n);
for(int i = 2; i <= n; i++)
if(is_prime(i))
a[t++] = i;
int num = 0;
for(int i = 1; i < t; i++) {
if(a[i] - a[i - 1] == 2)
num++;
}
printf("%d\n", num);
return 0;
}