Twin Prime Conjecture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1872 Accepted Submission(s): 576
Problem Description
If we define dn as: dn = pn+1-pn, where pi is the i-th prime. It is easy to see that d1 = 1 and dn=even for n>1. Twin Prime Conjecture states that "There are infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
Now given any positive integer N (< 10^5), you are supposed to count the number of twin primes which are no greater than N.
Input
Your program must read test cases from standard input.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
The input file consists of several test cases. Each case occupies a line which contains one integer N. The input is finished by a negative N.
Output
For each test case, your program must output to standard output. Print in one line the number of twin primes which are no greater than N.
Sample Input
1 5 20 -2
Sample Output
0 1 4/* 思路:孪生质数有一个定理就是:孪生素数猜想: 存在无穷多个素数 p, 使得 p+2 也是素数 */ #include<iostream> using namespace std; int p[100000]={0}; int visit[100000]; int main() { int n,i,j; for(i=0;i<100000;i++) visit[i]=0; for(i=2;i<330;i++) if(!visit[i]) for(j=i*i;j<100000;j+=i) visit[j]=1; for(i=4;i<100000;i++) if(!visit[i]&&!visit[i-2]) p[i]=p[i-1]+1; else p[i]=p[i-1]; while(1) { cin>>n; if(n<0) break; cout<<p[n]<<endl; } return 0; }