1007. 素数对猜想 (20)
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。
现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。
输入格式:每个测试输入包含1个测试用例,给出正整数N。
输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。
输入样例:20输出样例:
4
//32位操作系统上 int 的范围是 2147483647~-2147483648 所以即使不用 unsigned int 也不会溢出
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
int main()
{
vector<unsigned int> Odd(unsigned int);
unsigned int n;
cin >> n;
vector<unsigned int> AllOdd = Odd(n);
unsigned int total = 0;
for(vector<unsigned int>::size_type i = 1; i != AllOdd.size(); i++)
{
if(2 == AllOdd[i] - AllOdd[i-1])
total++;
}
cout << total << endl;
system("pause");
return 0;
}
vector<unsigned int> Odd(unsigned int n)
{
vector<unsigned int> outcome;
for(unsigned int i = 2; i<= n; i++)
{
bool flag = true;
for(unsigned int j = 2; j <= sqrt(i*1.0); j++)
{
if(0 == i%j)
flag = false;
}
if(flag)
outcome.push_back(i);
}
return outcome;
}