1007 素数对猜想 (20 分)
让我们定义dn为:dn=pn+1−pn,其中pi是第i个素数。显然有d1=1,且对于n>1有dn是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N
(<105),请计算不超过N
的满足猜想的素数对的个数。
输入格式:
输入在一行给出正整数N
。
输出格式:
在一行中输出不超过N
的满足猜想的素数对的个数。
输入样例:
20
输出样例:
4
#include <cstdio>
#include <cmath>
bool number(int a){
if (a <= 1) return false;
int x = (int) sqrt (1.0 * a); //若是不用sqrt函数,提交答案时提示会超时,sqrt函数的定义可以上百度查一下
for (int i = 2; i <= x; i++)
{
if (a % 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 (number(i) == true && number(i + 2) == true)
{
count++;
}
}
printf("%d", count);
return 0;
}