1不是素数,2是素数。
若判断n是否为素数,若 n 只存在 n%1 = 0, n%n = 0;则说明n为素数。
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
int s[100000];
int j=0;
//这个方法时间复杂度大些,时间会过
int judge(int n)
{
for(int i=2;i<n;i++)
{
if(n%i==0)
{
//cout<<"n:"<<n << " 不是素数"<<endl;
return 0;
}
}
//cout<<"n:" << n <<" 是素数"<<endl;
s[j] = n; j++;
return 1;
}
int judge_2(int n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i == 0)
{
return 0;
}
}
s[j] = n; j++;
//cout<<n<<" 为素数!"<<endl;
return 1;
}
int main()
{
for(int i=0;i<100000;i++)
s[i] = 0;
int n;
cin>>n;
//判断 n 是否为素数
for(int i=0;i<n;i++)
{
if((n-i)==1)
{
break;
}
//judge(n-i);
judge_2(n-i);
}
int count = 0;
for(int i=0;i<j;i++)
{
if(s[i]-s[i+1]==2 &&s[i]!=0 && s[i+1]!=0)
{
count++;
}
}
cout<<count;
}