POJ 2262-Goldbach's Conjecture(素数筛)
来源:互联网 发布:facebook代理软件fg 编辑:程序博客网 时间:2024/05/23 00:19
Goldbach's Conjecture
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39435 Accepted: 15119
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
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;int prime[1000010]={2,3,5};int k=3;int ip[1000010]={0};void is_prime(){ int i,j; int flag=0; int gad=2; ip[2]=1;ip[3]=1;ip[5]=1; for(i=7;i<=1000010;i+=gad){ flag=0; gad=6-gad; for(j=0;prime[j]*prime[j]<=i;j++){ if(i%prime[j]==0){ flag=1; break; } } if(!flag){ prime[k++]=i; ip[i]=1; } }}int main(){ int n,i,j; is_prime(); while(~scanf("%d",&n)){ if(!n) break; int f=0; for(i=0;i<=k;i++){ j=n-prime[i]; if(ip[j]){ f=1; break; } } if(!f) printf("Goldbach's conjecture is wrong.\n"); else printf("%d = %d + %d\n",n,prime[i],j); } return 0;}
0 0
- poj 2262 Goldbach's Conjecture 【素数筛】
- poj 2262 Goldbach's Conjecture 素数筛
- POJ 2262-Goldbach's Conjecture(素数筛)
- POJ 2262 Goldbach's Conjecture(素数表)
- poj 2262 Goldbach's Conjecture 素数 水题
- POJ 2262 Goldbach's Conjecture(素数相关)
- POJ 2262 Goldbach's Conjecture 素数筛选
- POJ 2262 Goldbach's Conjecture(素数)
- poj 2262 Goldbach's Conjecture 【素数筛法】
- POJ 2262 Goldbach's Conjecture ( 筛选法求素数 )
- POJ 2262/ ZOJ 1951:Goldbach\'s Conjecture - 筛法打素数表
- 素数水题 POJ 2262 Goldbach's Conjecture
- POJ 2262 Goldbach's Conjecture(素数筛选法)
- Poj.2262 Goldbach's Conjecture【素数】 2015/09/22
- POJ 2262 Goldbach's Conjecture(素数表分解质数)
- poj 2262 Goldbach's Conjecture -- 筛法求素数打表
- POJ 2262 Goldbach's Conjecture 素数打表
- POJ 2262 Goldbach's Conjecture 简单的素数
- Eclipse 使用
- 全局变量与局部变量
- .dll与.so文件的区别和构成
- Android事件分发完全解析之为什么是她
- NPY and shot(三分)
- POJ 2262-Goldbach's Conjecture(素数筛)
- mysql两表联合查询
- 什么是继承?
- poj_2002
- ACM Learning Languages
- 数组中只出现1次的两个数字
- 关于云服务器使用的若干~
- 算法(一) 两圆公切线
- linux scp复制命令