Description
Every even number greater than 4 can be
written as the sum of two odd prime numbers.
For example:
8 = 3 + 5. Both 3 and 5 are odd prime numbers.
20 = 3 + 17 = 7 + 13.
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.
Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.)
Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million.
Input
Each test case consists of one even integer n with 6 <= n < 1000000.
Input will be terminated by a value of 0 for n.
Output
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
解題思路
就是素數的查找和輸出
AC代碼
#include <cstdio>
int prime(int n)
{
int i;
for(i = 2; i * i <= n; i++)
if(n % i == 0)
return 0;
return 1;
}
int main()
{
int n, i, j;
while(scanf("%d", &n) && n)
{
for(i = 3; i <= n / 2; i += 2)
{
j = n - i;
if(prime(i) && prime(j))
{
printf("%d = %d + %d\n", n, i, j);
break;
}
}
}
return 0;
}