PAT-B-1007-素数对猜想

让我们定义dn为:dn=p**n+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有d*n*是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N(<105),请计算不超过N的满足猜想的素数对的个数。

输入格式:

输入在一行给出正整数N

输出格式:

在一行中输出不超过N的满足猜想的素数对的个数。

输入样例:

1
20

输出样例:

1
4

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;


bool isPrime(int n){
if(n<=1)return false;
int sqr=(int)sqrt(1.0*n);
for(int i=2;i<=sqr;i++)
if(n%i==0)return false;
return true;
}


int main(){

int n,count=0;
scanf("%d",&n);

//**关键思路:素数对一定是奇数对**
for(int i=3;i+2<=n;i+=2)
if(isPrime(i)&&isPrime(i+2))
count++;

printf("%d\n",count);
return 0;
}

----\(˙<>˙)/----赞赏一下吧~