7-55 Prime number pair conjecture (20 points)
Let us define d
n
as: d
n
=p
n+1
−p
n
, where p
i
is the i-th Prime number. Obviously d
1
=1, and d
n
is even for n>1 . The "prime pair conjecture" believes that "there are infinitely many pairs of adjacent prime numbers with a difference of 2".
Now given any positive integer N (<10
5
), please count the number of prime number pairs that do not exceed N that satisfy the conjecture.
Input format:
Input a positive integer N in one line.
Output format:
Output the number of prime number pairs that do not exceed N and satisfy the conjecture in one line.
Input sample:
20
Output sample:
4
Analysis: The
Ehrlich sieve 0 1 is not a prime number superposition prime number sieve;
#include<bits/stdc++.h>
using namespace std;
int prime[100009],book[100009];
void func(){
int t=0;
memset(book,1,sizeof(book));
book[0]=0,book[1]=0;
for(int i=0;i<=(100009);i++){
if(book[i]){
prime[t++]=i;
for(int j=2*i;j<=100009;j+=i)
book[j]=0;
}
}
}
int main(){
int n,sum=0;
cin>>n;
func();
for(int i=1;;i++){
if(prime[i]>n){
cout<<sum;
return 0;
}
if(prime[i]-prime[i-1]==2)sum++;
}
return 0;
}