https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744

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

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

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4
 代码:
#include <bits/stdc++.h>

using namespace std;

const int maxn=2e5+10;
int a[maxn]; int A(int x)
{
for(int i=2; i*i<=x; i++)
{
if(x%i==0)
return 0;
}
return 1;
}
int main()
{
int cnt=0;
for(int i=2;i<=maxn;i++)
{
if(A(i)==1)
{
cnt++;
a[cnt]=i;
}
}
int n;
scanf("%d",&n);
int ans=0;
for(int i=2;i<=n;i++)
{
if(a[i]-a[i-1]==2&&a[i]<=n)
//cout<<a[i]<<" ";
ans++;
}
printf("%d",ans);
return 0;
}

  

05-06 21:52
查看更多