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 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).
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
2
6
4
Case 1: 1
Case 2: 1
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, ...
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e7+1;
const int N=1e6;
bool a[maxn];
int prime[N],top;//top记录的是素数有几个,prinme里记录素数有哪几个
void aa()
{
top = 0;
a[1] = true;
for(int i = 2; i < maxn; i++)
{
if(a[i]) continue;
prime[top++] = i;
for(int j = 2; j * i < maxn; j++)//因为maxn是1e7,当i跑到1e7时,j=i*i则j会超过int的范围,这样写避免了开long long
a[j*i] = true;
}
}
//void aa()
//{
// top = 0;
// a[1] = true;
// for(long long i = 2; i < maxn; i++)
// {
// if(a[i]) continue;
// prime[top++] = i;
// for(long long j = i*i; j < maxn; j+=i)//如同以上所说,要开long long
// a[j] = true;
// }
//}
int main()
{
aa();
int t,x,c;
scanf("%d",&t);
c=0;
while(t--)
{
scanf("%d",&x);
int ans=0;
for(int i=0; i<top&&prime[i]<=x/2; i++)//直接从素数开始找
{
if(!a[x-prime[i]])//看x-素数是不是素数,是的话满足要求
ans++;
}
printf("Case %d: %d\n",++c,ans);
}
}