1007 素数对猜想(20 分)
让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N
(<105),请计算不超过N
的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N
。
输出格式:
在一行中输出不超过N
的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,j,m=0,count=0;
cin>>n;
int a[n];
for(i=2;i<=n;i++)
{
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
break;
}
}
if(j>sqrt(i))
{
a[m++]=i;//存储素数
}
}
for(i=0;i<m;i++)
{
if(a[i+1]-a[i]==2)
{
count++;
}
}
cout<<count<<endl;
return 0;
}