Goldbach's Conjecture
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35214   Accepted: 13493

Description

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
题目大意:给出一个6-1000000的偶整数n,判断是不是两个素数的和,如果是,按照要求的格式输出这两个素数,如果不是或者是n是奇数,输出“Goldbach's conjecture is wrong.”;输入0时退出程序。
这道题需要用到素数表,不过不用打印,只需要标记下谁是素数即可,所以只需要将打印素数表的代码稍微一修改就可以了。
 #include<iostream>
#include<math.h>
using namespace std;
int prime[]={};
int top=;
void print_prime()
{
int i;
double x=sqrt(1001000.0);
int n=int(x);
//cout<<sqrt(MAXN)<<endl;
//cout<<n<<endl;
for( i=; i<n; i++)
{
if(prime[i]==)
{
for(int j = i*i; j<; j+=i)
prime[j]=;
// prime[++top]=i;
}
}
/*for(i=n; i<MAXN; i++)
if(prime[i]==0)
prime[++top]=i;*/
}
int main()
{
print_prime();
int n;
while()
{
cin>>n;
if(n==)break;
if(n%==)
{
cout<<"Goldbach's conjecture is wrong."<<endl;
continue;
}
bool flag=true;
int i;
for(i=;i<=n/;i=i+)
{
if(prime[i]==&&prime[n-i]==)
{
cout<<n<<" = "<<i<<" + "<<n-i<<endl;
flag=false;
break;
}
}
if(flag==true)cout<<"Goldbach's conjecture is wrong."<<endl;
}
return ;
}

poj 2262【素数表的应用---判断素数】【哈希】的更多相关文章

  1. hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)2015 Multi-University Training Contest 10

    题意: 输入n,求c(n,0)到c(n,n)的所有组合数的最小公倍数. 输入: 首行输入整数t,表示共有t组测试样例. 每组测试样例包含一个正整数n(1<=n<=1e6). 输出: 输出结 ...

  2. POJ 2262 Goldbach&#39;s Conjecture(素数相关)

    POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示 ...

  3. LightOJ-1259-Goldbach`s Conjecture-素数打表+判断素数对数

    Goldbach's conjecture is one of the oldest unsolved problems in number theory and in all of mathemat ...

  4. <编程>比较两种素数表生成算法+计算程序运行时间+通过CMD重定向测试程序

    最近学习加密算法,需要生成素数表,一开始使用简单的循环,从2开始判断.代码如下: #include<iostream> #include<cstdio> #include< ...

  5. PAT Advanced 1059 Prime Factors (25) [素数表的建⽴]

    题目 Given any positive integer N, you are supposed to find all of its prime factors, and write them i ...

  6. 快速判断素数 --Rabin-Miller算法

    以前我在判断素数上一直只会 sqrt(n) 复杂度的方法和所谓的试除法(预处理出sqrt(n)以内的素数,再用它们来除). (当然筛选法对于判断一个数是否是素数复杂度太高) 现在我发现其实还有一种方法 ...

  7. 算法改进 | java语言中判断素数

    参考文章:http://blog.csdn.net/kp_liu/article/details/37569507 http://blog.csdn.net/huang_miao_xin/articl ...

  8. 2java判断素数

    package com.test; import java.math.*;import java.util.Scanner; public class test222 { /** * @param a ...

  9. filter运行出现 <filter object at 0x000001B68F052828> 判断素数

    刚接触filter时  运行总是出现<filter object at 0x000001B68F052828>  得不到想要的数据 后来发现是因为filter的结果是一个数组 需要 lis ...

随机推荐

  1. 百度地图api 常用demo

    功能一:获取map地图窗口的可视区域: var map = new BMap.Map("allmap");            // 创建Map实例 map.centerAndZ ...

  2. note:获取字符输入的一些函数

    总是弄混,所以总结一下: getline()     // 接受一个字符串,可以接收空格并输出,需包含“#include<string>” #include<iostream> ...

  3. CentOS 下安装xdebug

    在CentOS 6.x 的系统中,是集成xdebug 的, yum install PHP-pecl-xdebug 如果是CentOS.5 也可能通过安装安装 epel 来安装 rpm -ivh ht ...

  4. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  5. memcpy vs memmove

    [本文连接] http://www.cnblogs.com/hellogiser/p/memcpy_vs_memmove.html [分析] memcpy与memmove的目的都是将N个字节的源内存地 ...

  6. 一名Delphi程序员的开发习惯

    一名Delphi程序员的开发习惯 有关开发习惯的一些想法,如鲠在喉,不吐不快.究其发贴动机,当然不排除有骗取参与分的可能,但另一方面,也希望能给同行(念Xing)者提供一些 建议,或者参考(希望不是误 ...

  7. 解决VM虚拟机MAC OS X 10.10.x的卡顿问题

    点此链接下载beamoff安装到虚拟机即可.

  8. C Primer Plus_第一章_概览_复习题与编程练习

    REVIEW 1.就编程而言,可移植性表示什么? me 一个系统上编写的程序经过很少改动或者不需改动就可以在另一个系统上运行.如果修改是必须的,则通常只改变伴随主程序的一个头文件中的几项内容即可.(P ...

  9. linux下QT Creator常见错误及解决办法

    最近因为在做一个关于linux下计算机取证的小项目,需要写一个图形界面,所以想到了用QT来写,选用了linux下的集成开发环境QT Creator5.5.1,但刚刚安装好,竟然连一个"hel ...

  10. C#二维数组

    数组格式 一维数组: Console.WriteLine("输入班级人数"); int renshu = int.Parse(Console.ReadLine()); ; i &l ...