Offline calculation of all the structure within the prescribed range, not too difficult, mainly pay attention to efficiency, using sieve method to do.
The problem of twin primes
Time limit: ms | Memory limit:65535 KB
Difficulty:3
Describe
Write a program to find out the number of groups of all twin primes in the range of prime numbers. In general, the number of twins refers to the two prime number distance is 2, near the adjacent prime number can not be closer. Some children's shoes to see the problem began to write procedures, do not carefully read the question, we to curb the reading is not carefully carefully children's shoes, the two prime number adjacent to 1 is also a twin prime.
Input
The first line gives an N (0<n<100) indicating the number of test data groups.
The next set of test data gives m, which means finding out all the twin primes before M.
(0<m<1000000)
Output
Each set of test data output occupies one row, and the number of twins in the range of M ranges.
Sample input
114
Sample output
4
#include <iostream> #include <cstring> #include <cmath>using namespace Std;int a[1000000];void IsPrime (int number) {int i,j;for (i=1;i<=number;i++) if (i%2) a[i]=1;for (i=3;i<=sqrt (number); i+=2) if (A[i]) {for ( j=i*2;j<=number;j+=i) a[j]=0;}} int main () {int N,m,i,count;cin>>n;while (n--) {Cin>>m;memset (a,0,sizeof (a)); Count=0;isprime (m); for (i=2 ; i<=m-2;i++) if (A[i] && a[i+2])//Determine whether it is a prime number count++;if (m>=3) Count++;cout<<count<<endl;} return 0;}
NYOJ26 Twin Prime problems