当前位置: 代码迷 >> C语言 >> [求助]一个偶数表示两个奇数之和。
  详细解决方案

[求助]一个偶数表示两个奇数之和。

热度:227   发布时间:2006-11-05 16:27:36.0
[求助]一个偶数表示两个奇数之和。

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 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

搜索更多相关的解决方案: 偶数  之和  Euler  奇数  conjecture  

----------------解决方案--------------------------------------------------------
If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized
----------------解决方案--------------------------------------------------------
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.
If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized
这里的规律是什么呀?
----------------解决方案--------------------------------------------------------

纠正一下:应该是一个偶数表示成为两个质数之和(num=a+b).当有很多组表示时,取使得b-a取到最大的那组.

所以比较简单:
直接a从3开始找(刚开始判断num!=4,若为4,直接2+2).只要a,num-a都是质数,则结束循环.否则a+=2,循环结束条件为(a<num/2);


----------------解决方案--------------------------------------------------------

老感觉是ACM里的题目,这些是楼主老师弄来的题目?
顺便问一下,楼主是学计算机的?大几?


----------------解决方案--------------------------------------------------------
呵呵!原来是质数之和啊!但是怎样判断一个数十质数啊?
直接a从3开始找(刚开始判断num!=4,若为4,直接2+2).只要a,num-a都是质数,则结束循环.否则a+=2,循环结束条件为(a<num/2);
这句话我没有理解!麻烦你再讲的仔细一些!谢谢了!
----------------解决方案--------------------------------------------------------
呵呵!我是大一的,这些确实都是ACM的题,我学的是软件专业!才学C语言不久!
----------------解决方案--------------------------------------------------------
哪个大学ACM的.
我来解释下:
其实也蛮简单的,如果是4,那只有一种情况那就是4=2+2.
所以可以直接在3开始,a+=2,是因为只有奇数才有可能是质数.要求b-a最大,所以只要a最小,那么b-a就会最大.

质数:只能整除1和本身的整数.(1不是质数)
----------------解决方案--------------------------------------------------------
6 &lt;= n &lt; 1000000取值范围已经有了,所以不用判断是不是4的情形了!
----------------解决方案--------------------------------------------------------
哈工大的
----------------解决方案--------------------------------------------------------
  相关解决方案