Twin Prime conjecture
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Problem descriptionif We define DN AS:DN = PN+1-PN, where pi is the i-th prime. It's easy-to-see-D1 = 1 and Dn=even for n>1. Twin Prime conjecture states that "there is infinite consecutive primes differing by 2".
Now given any positive integer N (< 10^5), you is supposed to count the number of twin primes which is no greater tha n N.
Inputyour 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.
Outputfor Each test case, the your program must the output to standard output. Print in one line the number of twin primes which is no greater than N.
Sample input1520-2
Sample Output014
SOURCE Zhejiang University Computer Postgraduate Security Research Exam-2011 Test Instructions: The logarithm of the number of twin primes within N;
Thought: This thought that the prime number dozen table + violence can be over, the result data seems a lot, seek the prefix and make a table on the line;
#include <bits/stdc++.h>using namespacestd;#definell Long Long#defineMoD 1000000007#definePi (4*atan (1.0))Const intn=1e5+Ten, m=1e6+Ten, inf=1e9+Ten;Const intmaxn=100001;intPRIME[MAXN];BOOLVIS[MAXN];intPrime (intN) { intCnt=0; memset (Vis,0,sizeof(VIS)); for(intI=2; i<n;i++) { if(!vis[i]) prime[cnt++]=i; for(intj=0; j<cnt&&i*prime[j]<n;j++) {vis[i*prime[j]]=1; if(i%prime[j]==0) Break; } } returnCNT;}intFlag[n];intSum[n];intMain () {intli{Prime (MAXN); intx,y,z,i,t; for(i=1; i<ji;i++) { if(prime[i]-prime[i-1]==2) Flag[prime[i]]=1; } for(i=1; i<=100000; i++) Sum[i]=sum[i-1]+Flag[i]; while(~SCANF ("%d",&x)) {if(x<0) Break; printf ("%d\n", sum[x]); } return 0;}
Hdu 3792 Twin Prime conjecture prefix and + Euler hit table