LightOJ-1259-Goldbach`s Conjecture- prime punch table + judgment prime 对数

Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathematics. It states:

Every even integer, greater than 2, can be expressed as the sum of two primes [1].

Now your task is to check whether this conjecture holds for integers up to 107.

Input

Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case starts with a line containing an integer n (4 ≤ n ≤ 107, n is even).

Output

For each case, print the case number and the number of ways you can express n as sum of two primes. To be more specific, we want to find the number of (a, b) where

1)      Both a and b are prime

2)      a + b = n

3)      a ≤ b

Sample Input

2

6

4

Sample Output

Case 1: 1

Case 2: 1

Note

  1. An integer is said to be prime, if it is divisible by exactly two different integers. First few primes are 2, 3, 5, 7, 11, 13, ...

 

  • The meaning of problems: given number n, is determined by the number of pairs of each of the compositions primes

 

  • Note: pri open array range

      Play table marked with bool, RT has been otherwise

      Determining when the number of prime numbers come n / 2 behind certainly can not find one pair, and this time can break up

 

There are a few of the prime judge:

for(int i=0; i<p; i++)
{
    if(pri[i]>n/2)
        break;
    if(book[n-pri[i]]==0)
        ans++;
}

 

 

 1 #include<stdio.h>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<string.h>
 5 typedef long long ll;
 6 using namespace std;
 7 
 8 //const int N=1e7+20;
 9 bool book[10000001];
10 int pri[5000000];
11 int p;
12 
13 void prime()
14 {
15     //非素数标记为1
16     //memset(book,0,sizeof(book));
17      // memset (PRI, 0, sizeof (PRI)); here they will empty, it should not go empty, consuming 
18      Book [ 0 ] = 1 ;
 19      Book [ 1 ] = 1 ;
 20      the p-= 0 ;
 21      for ( int I = 2 ; I <= 10000000 ; I ++ )
 22 is      {
 23 is          IF (Book [I] == 0 )
 24          {
 25              PRI [P ++] = I; // record prime number of elements 
26 is              for ( int J = I * 2 ; j <=10000000; j+=i)
27                 book[j]=1;
28         }
29     }
30 }
31 
32 int main()
33 {
34     prime();
35     // sort(pri,pri+p);
36     int t,tt=1;
37     scanf("%d",&t);
38     int n;
39     while(t--)
40     {
41         scanf("%d",&n);
42         int ans=0;
43         for(int i=0; i<p; i++)
44         {
45             if(pri[i]>n/2)
46                 break;
47             if(book[n-pri[i]]==0)
48                 ans++;
49         }
50         printf("Case %d: %d\n",tt++,ans);
51     }
52     return 0;
53 }

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/11330546.html