[B] PAT for prime conjecture

Subject description:

Let us define D n- as: D n- = P n-+. 1 -p n- , where P i is the i-th prime number. Clearly D . 1 =. 1, and for n> 1 with a D n- is an even number. "Primes to guess" that "there is an infinite number of adjacent and poor is a prime number 2."

Now given an arbitrary positive integer N (<10 . 5 ), does not exceed Calculate number N satisfies the conjecture of primes.

Input formats:

It gives a positive integer in the input line N.

Output formats:

In the output line does not exceed the number N satisfy the conjecture of the primes.

Sample input:

20    

Sample output:

4

Problem-solving ideas:

First, find the prime numbers, cycle order to identify the phase difference of two prime numbers, the number of statistics.

Code:

#include<iostream>
using namespace std;
int a[100000]={0};
int main() {
    int count=0;
    long n;
    for(int i=2;i<50001;i++) {
        for(int j=2;i*j<100000;j++) {
            if(a[i*j]==0) {
                a[i*j]=1;
            }
        }
    }
    cin>>n;
    for(int i=2;i<=n-2;i++) {
        if(a[i]==0&&a[i+2]==0) {
            count++;
        }
    }
    cout<<count<<endl;
    return 0;
}
Published 55 original articles · won praise 30 · views 9807

Guess you like

Origin blog.csdn.net/chaifang0620/article/details/104914271