1024programmer Java ZOJProblemSet–1951Goldbach’sConjecture

ZOJProblemSet–1951Goldbach’sConjecture

Time Limit: 2 Seconds Memory Limit: 65536 KB


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
0

Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37


Source: University of Ulm Local Contest 1998

 1: #include
 
 2: #include
 
 3: #include
 
 4:
 
 5: using namespace std;
 
 6:
 
 7: const int PRIME_NUM = 1000000;
 
 8:
 
 9: bitset Primes;
 
 10: void InitialPrimers()
 
 11: {
 
 12: Primes.set();
 
 13: int i,k;
 
 14: for(i=2;i<PRIME_NUM;i++)
 
 15: {
 
 16: k=2*i;
 
 17: while(k<PRIME_NUM)
 
 18: {
 
 19: if(Primes.test(k))
 
 20: Primes.reset(k);
 
 21: k+=i;
 
 22: }
 
 23: }
 
 24: }
 
 25:
 
 26: int main(void)
 
 27: {
 
 28: InitialPrimers();
 
 29: int n;
 
 30:
 
 31: while(cin>>n && n)
 
 32: {
 
 33: bool find = false;
 
 34: for(int i = 3;i <= n/2  ;i+=2)
 
 35: {
 
 36: if(Primes.test(n - i) && Primes.test(i))
 
 37: {
 
 38: cout<<n<<" = "<<i<<" + "<<n - i<<endl;
 
 39: find = true;
 
 40: break;
 
 41: }
 
 42: }
 
 43: if(!find)
 
 44: cout<<"Goldbach's conjecture is wrong."<<endl;
 
 45: }
 
 46: return 0;
 
 47: }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/768498

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索