1. 程式人生 > >1007 素數對猜想(原創)

1007 素數對猜想(原創)

1007 素數對猜想(20 分)
讓我們定義d
​n
​​為:d
​n
​​=p
​n+1
​​−p
​n
​​,其中p
​i
​​是第i個素數。顯然有d
​1
​​=1,且對於n>1有d
​n
​​是偶數。“素數對猜想”認為“存在無窮多對相鄰且差為2的素數”。
現給定任意正整數N(<10
​5
​​),請計算不超過N的滿足猜想的素數對的個數。N。
輸出格式:
在一行中輸出不超過N的滿足猜想的素數對的個數。
輸入樣例:
20
輸出樣例:
4

#include <iostream>
#include<bits/stdc++.h>
using namespace std; int Isprime(int a) { int flag=1; for(int i=2;i<=sqrt(a);i++) { if(a%i==0||a<2) { flag=0; } } return flag; } int main() { int n,j; int count=0; cin>>n; for(j=2;j<n-1;j++) { if(Isprime(j)==1
) { if(Isprime(j+2)==1) { count++; } } } cout << count << endl; return 0; }