Goldbach`s Conjecture (LightOJ - 1259) [simple] [number theory sieve]

Goldbach`s Conjecture (LightOJ - 1259) [simple] [number theory sieve]

Tags: Getting the stops on the lecture topic


Title Description

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

Goldbach conjecture: arbitrary even number not less than 2, can be split into two and added in the form of prime numbers. (The suspect has not been yet complete proof of authenticity.)

Give you a \ (n \) , Q \ (n \) in accordance with the Goldbach conjecture There are several methods of resolution? \ ((n \ leq 1e7) \)

Resolve

Although it is impossible to prove Goldbach's conjecture, but in a smaller range of data, we can find the solution by computer.

We first find by the linear sieve \ (\ 1e7) Number of full-constitution within no more than \ (67e4 \) months so that we For each \ (\ n) cycle again prime number, just to see \ (n \) and the difference in the number of prime numbers or whether the quality of it. This difference if is a prime number, then one more embodiment.
Since we there \ (vis [1 \ cdots n ] \) array, can be \ (O (1) \) determines the difference is not a prime number request.

Note: This question can be used Euler screen can also be used Erichsen screen (see vjudge I submit the code has Erichsen screen).

Discussion contents: prime 筛法

By Code

/*
Problem
    LightOJ - 1259
Status
    Accepted
Time
    236ms
Memory
    14468kB
Length
    798
Lang
    C++
Submitted
    2019-11-24 22:23:13
Shared
    
RemoteRunId
    1640384*/

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e7 + 50;

int prime[670000], cnt = 0;    //注意这题有内存限制,prime数组开到1e7会RE(MLE).我提前打表看了一下,1e7以内的质数不超过67e4个.
bool vis[MAXN];

void init()     //欧拉筛。线性时间复杂度内筛出质数来.prime数组存质数,vis[i]数组判断i是否为质数.
{
    vis[1] = 1;

    for(int i = 2; i <= int(1e7 + 5); i ++){

        if(!vis[i])
            prime[++ cnt] = i;

        for(int j = 1; j <= cnt && i * prime[j] <= int(1e7 + 5); j ++){
            vis[i * prime[j]] = 1;
            if(i % prime[j] == 0)
                break;
        }
    }

    return ;
}

int main()
{
    init();  

    int T, n, times = 0;
    
    scanf("%d", &T);
    
    while(T --){
        scanf("%d", &n);

        int ans = 0;
        for(int i = 1; i <= cnt && prime[i] <= n / 2; i ++){    //只需循环到n/2即可.在(n/2, n]区间中出现的序偶<a, b>,在[2, n/2]区间也一定出现过.

            if(!vis[n - prime[i]]){
                ans ++;
           }
        }
        printf("Case %d: %d\n", ++ times, ans);
    }
    return 0;

}

Guess you like

Origin www.cnblogs.com/satchelpp/p/11924964.html