Pat math prime
Link: http://pat.zju.edu.cn/contests/pat-b-practise/1007
Let's define DN as: DN = pN + 1-PN, where Pi is the I prime number. Obviously, d1 = 1 and for n> 1, dn is an even number. The "prime number pair conjecture" considers "There are infinite numbers of adjacent prime numbers with a difference of 2 ".
Given any positive integer N (<105), calculate the number of prime number pairs that do not exceed n.
Input Format:Each test input contains one test case and a positive integer n.
Output Format:The output of each test case occupies one row. The number of prime number pairs that meet the conjecture cannot exceed n.
Input example:
20
Output example:
4
Ideas:
First, we can list all the prime numbers of the first 10000, and then determine the number of adjacent prime numbers with a difference of 2 from 1 to n!
The Code is as follows:
#include <cstdio>#include <cstring>int a[100017];void init(){ memset(a,0,sizeof(a)); for(int i = 2; i <= 100000; i++) { if(a[i] == 0) for(int j = i+i; j <= 100000; j+=i) { a[j] = 1; } }}void solve(int n){ int k = 0; int tt = 2; for(int i = 3; i <= n; i++) { if(a[i] == 0) { if(i-tt == 2) k++; tt = i; } } printf("%d\n",k);}int main(){ int n; init(); while(~scanf("%d",&n)) { solve(n); } return 0;}
1007. Conjecture of prime number pairs (20) (zjupat mathematics)