B1007. 素数对猜想 (20)

让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数。显然有 d1=1 且对于n>1有 dn 是偶数。“素数对猜想”认为“存在无穷多对相邻且差为2的素数”。

现给定任意正整数N (< 105),请计算不超过N的满足猜想的素数对的个数。

输入格式:每个测试输入包含1个测试用例,给出正整数N。

输出格式:每个测试用例的输出占一行,不超过N的满足猜想的素数对的个数。

输入样例:

20

输出样例:

4

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <algorithm>
 8 using namespace std;
 9 
10 bool isPrime(int n)
11 {
12     if(n<=1)return false;
13     int sqr=sqrt(1.0*n);
14     for(int i=2;i<=sqr;i++)
15     {
16         if(n%i==0)return false;
17     }
18     return true;
19 } 
20 //素数表 
21 const int maxn=100000;
22 int prime[maxn]={0},num=0;
23 bool p[maxn]={0};
24 void Find_Prime(int n)
25 {
26     for(int i=2;i<maxn;i++)
27     {
28         if(p[i]==false)
29         {
30             prime[num++]=i;
31             if(num>n)break;
32             for(int j=i+i;j<maxn;j+=i)
33             {
34                 p[j]=true;
35             }
36         }
37     }
38 } 
39 
40 int main(){
41     
42     int n,count=0;
43     scanf("%d",&n);
44     Find_Prime(n+1);
45     for(int i=3;i+2<=n;i+=2)
46     {
47         if(p[i]==false&&p[i+2]==false)
48         count++;
49     }
50     printf("%d",count);
51     return 0;
52 }
原文地址:https://www.cnblogs.com/ligen/p/4305483.html