#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
bool isPrime(int n)
{
bool flag = true;
for(int i = 2; i <= sqrt(n); ++i)
{
if(n % i == 0)
{
flag = false;
break;
}
}
return flag;
}
int main()
{
int num;
cin>>num;
vector<int> primeVec;
int count = 0;
bool flag;
for(int i = 2;i <= num; i++)
{
if(isPrime(i))
primeVec.push_back(i);
}
for(size_t j = 0; j < primeVec.size() - 1;j++)
{
if((primeVec[j + 1] - primeVec[j]) == 2)
count++;
}
cout<<count;
return 0;
}
下面有作者总结的素数判断及相应耗时对比,本文使用的第二种