Goldbach's Conjecture |
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
Every number greater than 2 can be written as the sum of three prime numbers.
Goldbach cwas considering 1 as a primer number, a convention that is no longer followed. Later on, Euler re-expressed the conjecture as:
Every even number greater than or equal to 4 can be expressed as the sum of two 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 as expressed by Euler for all even numbers less than a million.
Input
The input file will contain one or more test cases.
Each test case consists of one even integer n with .
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 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
Miguel A. Revilla
1999-01-11
验证哥德巴赫猜想,给一个大于等于4的偶数,将它分解成两个素数之和。
先求出0~1000000之间的所有素数,然后从小到大试验即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 bool isPrime[1000050]; 8 int prime[80000]; 9 10 int initial() 11 { 12 for(int i=1;i<=1000000;i++) 13 isPrime[i]=true; 14 isPrime[0]=isPrime[1]=false; 15 16 for(int i=4;i<=1000000;i+=2) 17 isPrime[i]=false; 18 for(int i=3;i<=1000;i+=2) 19 if(isPrime[i]) 20 for(int j=i*2;j<=1000000;j+=i) 21 isPrime[j]=false; 22 23 int t=0; 24 for(int i=1;i<=1000000;i++) 25 if(isPrime[i]) 26 prime[t++]=i; 27 28 return t; 29 } 30 31 int main() 32 { 33 int t=initial(); 34 35 int n; 36 37 while(scanf("%d",&n)==1&&n) 38 { 39 int a=-1; 40 for(int i=0;i<t;i++) 41 { 42 if(isPrime[n-prime[i]]) 43 { 44 a=prime[i]; 45 break; 46 } 47 } 48 if(a!=-1) 49 printf("%d = %d + %d ",n,a,n-a); 50 else 51 puts("Goldbach's conjecture is wrong."); 52 } 53 54 return 0; 55 }