Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 35054 | Accepted: 13441 |
Description
Every even number greater than 4 can be
written as the sum of two odd prime numbers.
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.
Input
Output
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
#include <stdio.h> #include <stdlib.h> #define MAX 1000001 int prime[MAX]={1}; void ProductData() { int i, j, k; for(i = 2; i*i <= MAX; i++) { // 排除偶数的情况 if( i!=2 && i%2==0) continue; // 排除不必要的奇数判断 奇数相乘一定是两个奇数 for(k = 3; k*k <= i; k += 2) if(i%k == 0) break; if(k*k <= i) continue; // 筛选法 除去 i 的倍数 if(prime[i] == 1) for(j = 2*i; j <= MAX; j+=i) prime[j] = 0; } } int main() { int n, i; ProductData(); while(scanf("%d",&n)!=EOF && n!= 0) { if(n%2 != 0 || n < 6) return 0; for(i = 3; i*i <= n; i+=2) { if(prime[i] && prime[n-i]) { printf("%d = %d + %d\n",n, i, n-i); break; } } if(i*i > n) printf("Goldbach's conjecture is wrong.\n"); } return 0; }