问题 G: Goldbach's Conjecture
时间限制: 1 Sec 内存限制: 32 MB
提交: 9 解决: 9
[提交][状态][讨论版]
题目描述
Goldbach's Conjecture: For any evennumber n greater than or equal to 4, there exists at least one pair of primenumbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whetherthis conjecture actually holds. However, one can find such a pair of primenumbers, if any, for a given even number. The problem here is to write aprogram that reports the number of all the pairs of prime numbers satisfyingthe condition in the conjecture for a given even number.
A sequence of even numbers is given asinput. Corresponding to each number, the program should output the number ofpairs mentioned above. Notice that we are interested in the number ofessentially different pairs and therefore you should not count (p1, p2) and(p2, p1) separately as two different pairs.
输入
An integer is given in each input line.You may assume that each integer is even, and is greater than or equal to 4 andless than 2^15. The end of the input is indicated by a number 0.
输出
Each output line should contain aninteger number. No other characters should appear in the output.
样例输入
4
10
16
0
样例输出
1
2
2
#include <stdio.h>
#include <math.h>
#define N 100000
int a[N];
void getprime()
{
int i, j;
a[1]=0;
a[2]=1;
for(i=3; i<=N; i++)
{
if(i%2==0)
a[i]=0;
else
a[i]=1;
}
int t=sqrt(N);
for(i=3; i<=t; i++)
{
if(a[i])
{
for(j = 2*i; j<=N; j+=i)
a[j]=0;
}
}
}
int main()
{
int i, count,n;
getprime();
while(scanf("%d", &n),n)
{
for(count =0, i=2; i<=n/2; i++)
{
if(a[i]&&a[n-i])
count++;
}
printf("%d\n", count);
}
return 0;
}