/* 1007. Prime Pair Conjecture (20) Let us define dn as: dn = pn+1 - pn, where pi is the ith prime number. Obviously d1=1 and dn is even for n>1. The "prime pair conjecture" states that "there are infinitely many pairs of adjacent prime numbers that differ by 2". Now given any positive integer N (< 10^5), please count the number of prime pairs that satisfy the conjecture not exceeding N. Input format: Each test input contains 1 test case, giving a positive integer N. Output format: The output of each test case occupies one line, and does not exceed the number of N prime pairs that satisfy the conjecture. Input sample: 20 Sample output: 4 */ /* Ideas: 1. Enter a positive integer n; 2. Create a prime-marked integer array prime_array[n+1], and initialize the element to 0; 3. Starting from 2, that is, prime_array[2], traverse the elements of the array prime_array to n in turn (+1), and determine whether each element is a prime number. If the current element is a prime number, mark prime_array[i] as 1; 4. Determine whether prime_array[i-2] is marked as a prime number, if so, count it, otherwise, execute the next bit. the complexity: O(n^2); */ #include<stdio.h> #include<math.h> bool isPrime(int n){ int k; k = sqrt(n); for(int j=2;j<=k;j++){ if(n%j==0) return false; } return true; } int main(){ int n,prime_count = 0; int *prime_array;//Marked array with memory function. scanf("%d", &n); prime_array = new int [n+1]; if(n>1){ prime_array[0]=0; prime_array[1]=0; } for(int i=2;i<=n;i++){//Note: equal sign, details! prime_array[i]=0;//Initialize prime mark element if(isPrime(i) == true){ prime_array[i] = 1; if(prime_array[i-2] == 1) prime_count++; } } //test // for(int i=0;i<n;i++){ // printf("%d ", prime_array[i]); // } // printf("\n"); printf("%d", prime_count); }