Zoj problem set-1951 Goldbach's Conjecture

Source: Internet
Author: User

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 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 in 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 shocould 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:Universities of Ulm Local contest 1998

 
1: # include <iostream>
2: # include <bitset>
 
3: # include <cmath>
 
4:
 
5:Using NamespaceSTD;
 
6:
7:Const IntPrime_num= 1000000;
 
8:
 
9: bitset <prime_num> primes;
 
10:VoidInitialprimers ()
 
11 :{
12: primes. Set ();
 
13:IntI, 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:IntMain (Void)
27 :{
 
28: initialprimers ();
 
29:IntN;
 
30:
 
31:While(CIN> N & N)
32 :{
 
33:BoolFind =False;
 
34:For(IntI = 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:Return0;
 
47 :}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.