Goldbach's Conjecture
来源:互联网 发布:乐视如何查看mac码 编辑:程序博客网 时间:2024/05/23 02:24
Goldbach's Conjecture
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 5
Problem Description
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
For example:
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.
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.
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
820420
Sample Output
8 = 3 + 520 = 3 + 1742 = 5 + 37
Source
PKU
代码如下:
#include <iostream>#include <cstdio>#include <cmath>using namespace std;const int N = 1000000;const int M = 700;int prime[M];bool is_prime[M];void do_prime(){ int nprime = 0; for(int i = 1; i < M; i ++) { is_prime[i] = true; } is_prime[1] = false; for(int i = 2; i < M; i ++) { if(is_prime[i]) { prime[nprime++] = i; for(int j = i*i; j < M; j += i) { is_prime[j] = false; } } }}bool Prime(int x){ for(int i = 2; i <= (int)sqrt(x); i ++) if(x % i == 0) return false; return true;}int main(){ int n ; do_prime(); while(scanf("%d",&n),n) { for(int i = 0; prime[i] < n; i ++) { if(Prime(n-prime[i])) { printf("%d = %d + %d\n",n,prime[i],n-prime[i]); break; } } } return 0;}
0 0
- Goldbach's Conjecture
- poj2262 Goldbach's Conjecture
- Goldbach's Conjecture
- Goldbach's Conjecture
- HOJ1459 Goldbach's Conjecture
- POJ2262 Goldbach's Conjecture
- 2262 Goldbach's Conjecture
- Goldbach's Conjecture
- POJ2262:Goldbach's Conjecture
- HDU1397:Goldbach's Conjecture
- Goldbach's Conjecture
- Goldbach's Conjecture
- poj2262 Goldbach's Conjecture
- poj_2262 Goldbach's Conjecture
- zoj_1657 Goldbach's Conjecture
- poj2262-Goldbach's Conjecture
- POJ2909:Goldbach's Conjecture
- K - Goldbach's Conjecture
- C++程序设计项目开发——银行自动提款机(一)
- Benchmark of Python WSGI Servers
- 跨函数使用内存
- blog
- 24.贱啊
- Goldbach's Conjecture
- perl模块之Smart::Comments
- iOS7 UIKit Dynamics
- hdu 1394 Minimum Inversion Number_归并排序
- 游戏对象的实现
- 从AndroidManifest.xml中获取已注册Activity的方法
- IOS设计模式的六大设计原则之单一职责原则(SRP,Single Responsibility Principle)
- "海潮"我的排序算法设计
- 寻找素数对