一、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."

二、题解

        一看到这个题目的时候思路还是很清晰的,心想很快就能做完的,但是没想到WA了不下6次。那个自己写的判断奇素数的方法测试数据都没有问题,但不知道怎么回事就是过不了。后来用了别人用的判断素数的方法,最后过了。最简单的方法:用n除以2 ~ n^2,有一个能除尽就不是素数,否则是素数。时间复杂度:O(sqrt(n))。题目不难,但有些方法的运用还是存在考虑不全的情况。这里有详细的素数判断方法介绍http://wxdlut.blog.163.com/blog/static/128770158200910129412537/

       poj2909与此题相同,只是输出的是等式的个数。

三、Java代码

import java.util.Scanner;

public class Main {
public static boolean isOddPrime(int a){
for(int i=2;i*i<=a;i++){
if(a % i==0)
return false;
}
return true;
}
public static void conjecture(int n){
int i=3;
while(i<=n){
if(isOddPrime(i) && isOddPrime(n-i)){
System.out.println(n+" = "+i+" + "+(n-i));
break;
}
i+=2;
}
}
public static void main(String[] args){
Scanner cin=new Scanner(System.in);
int n;
while((n=cin.nextInt())!=0){
conjecture(n);
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 2662,2909 Goldbach's Conjecture (素数判定)的更多相关文章

  1. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. LightOJ 1259 Goldbach`s Conjecture 素数打表

    题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...

  3. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

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

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

  5. Poj 2262 / OpenJudge 2262 Goldbach's Conjecture

    1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...

  6. HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)

    Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...

  7. 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429

    素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...

  8. POJ 2262 Goldbach's Conjecture (打表)

    题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...

  9. F - Goldbach`s Conjecture 对一个大于2的偶数n,找有多少种方法使两个素数的和为n;保证素数a<=b; a+b==n; a,b都为素数。

    /** 题目:F - Goldbach`s Conjecture 链接:https://vjudge.net/contest/154246#problem/F 题意:对一个大于2的偶数n,找有多少种方 ...

随机推荐

  1. 九度OJ 1354:和为S的连续正数序列 (整除)

    时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:2028 解决:630 题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不 ...

  2. 学院名单-211院校研招学院-中国教育在线(www.eol.cn)170915164402

    [数据结果] 学校数.学院数:112,2657. [数据来源] 中国教育在线(www.eol.cn)211院校研招学院. http://www.eol.cn/html/ky/gxmd/211.shtm ...

  3. 页游手游服务器(五)sql缓存层

    sql的通用缓存,是实现最麻烦的一部分,对于查询结果的缓存,主要有如下的结构来缓存: cache tablename--player statement--select * from player w ...

  4. hdu 3718 Different Division

    Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. 在线工具集合(新增cron quartz表达式在线生成……)

    缘起 平时工作,须要一些工具.经过一些使用,对照,保留一些比較方便好用的在线工具 工具会持续更新中.. . 在线编译&&反编译  http://www.showmycode.com/ ...

  6. win7旗舰版 安装IIS中出现的问题

    最好先安装IIS成功了,再安装VS2010或者别的版本 1.hppt 错误500.19,-Internal server erroe,无法访问的请求野蛮,因为该页的相关配置数据无效,HTTP Erro ...

  7. Eclipse +Tomcat配置-【菜鸟学JAVA】

    说起来也惭愧,以前用(Unieap)工具搞了一年多的JAVA+Oracle,现在居然在Eclipse中配置tomcat都搞了半天时间,原来一直是做.NET的. 今天开始不再用集成的环境,话不多说,开始 ...

  8. VIM YCM 插件安装问题记录

    参考:https://github.com/yangyangwithgnu/use_vim_as_ide https://github.com/Valloric/YouCompleteMe 根据 ht ...

  9. Kattis - fire2 【BFS】

    第二组样例: 题意 给出一个起始位置,然后要跑到这幢建筑物的外面,上下左右四个方向,只要是空地 就可以走 走一步 花费一秒 然后有若干串火苗,每一秒钟 会向上下左右 四个方向的空地 蔓延 但是 逃跑的 ...

  10. Java多线程系列 JUC线程池02 线程池原理解析(一)

    转载  http://www.cnblogs.com/skywang12345/p/3509960.html ; http://www.cnblogs.com/skywang12345/p/35099 ...