1.Link:
http://poj.org/problem?id=2262
http://bailian.openjudge.cn/practice/2262
2.Content:
Goldbach‘s Conjecture
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37791 Accepted: 14536 Description
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: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
The input will contain one or more test cases.
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
For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach‘s conjecture is wrong."Sample Input
8 20 42 0Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37Source
3.Method:
筛素数法
4.Code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 6 #define MAX_NUM 1000000 7 8 using namespace std; 9 10 int main() 11 { 12 //freopen("D://input.txt","r",stdin); 13 14 int i,j; 15 16 bool * arr_prime = new bool[MAX_NUM + 1]; 17 18 for(i = 3; i <= MAX_NUM; i += 2) arr_prime[i] = true; 19 for(i = 4; i <= MAX_NUM; i += 2) arr_prime[i] = false; 20 arr_prime[2] = true; 21 22 int sqrt_mn = sqrt(MAX_NUM); 23 for(i = 3; i <= sqrt_mn; i += 2) 24 { 25 if(arr_prime[i]) 26 { 27 for(j = i + i; j <= MAX_NUM; j += i) arr_prime[j] = false; 28 } 29 } 30 31 int a; 32 cin >> a; 33 34 while(a != 0) 35 { 36 37 if(a % 2 == 0 && arr_prime[a - 2]) 38 { 39 cout << a << " = " << "2" << " + " << (a - 2) << endl; 40 } 41 else 42 { 43 for(i = 3; i <= a / 2; i += 2) 44 { 45 if(arr_prime[i] && arr_prime[a - i]) 46 { 47 cout << a << " = " << i << " + " << (a - i) << endl; 48 break; 49 } 50 } 51 } 52 53 cin >> a; 54 } 55 56 return 0; 57 }
5.Reference: