Poj 2662,2909 Goldbach's Conjecture (素数判定)
一、Description
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
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
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 (素数判定)的更多相关文章
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- LightOJ 1259 Goldbach`s Conjecture 素数打表
题目大意:求讲一个整数n分解为两个素数的方案数. 题目思路:素数打表,后遍历 1-n/2,寻找方案数,需要注意的是:C/C++中 bool类型占用一个字节,int类型占用4个字节,在素数打表中采用bo ...
- poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...
- POJ 2262 Goldbach's Conjecture(素数相关)
POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示 ...
- Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach ...
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...
- POJ 2262 Goldbach's Conjecture (打表)
题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...
- 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,找有多少种方 ...
随机推荐
- iOS解决导航引起视图高度问题
经过导航栏跨越的坑,总结出有两种方法可以无痕解决(前提>=iOS7版本)(TabBar与导航栏类似) 1.通过设置导航栏的透明度实现(这种方式的控制器view的起始坐标是充(0,64)开始的) ...
- 修改maven的war包生成路径
因为要配合jenkins,所以控制了war包的生成目录: <plugins> <!--打war包到指定的目录下 --> <plugin> <groupId&g ...
- 【译】常见 Java 异常解释(恶搞版)
常见 Java 异常解释:(译者注:非技术角度分析.阅读有风险,理解需谨慎o(╯□╰)o) java.lang ArithmeticException 你正在试图使用电脑解决一个自己解决不了的数学问题 ...
- JNDI知道这么多就够了!
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sunshoupo211/article/details/37924017 JNDI 全称:Java ...
- CentOS iSCSI服务器搭建------Target篇
先上服务器信息(当然是我YY的服务器.哈哈) [root@node ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@node ...
- Linux ~ termios 串口编程
ermios 结构是在POSIX规范中定义的标准接口,它类似于系统V中的termio接口,通过设置termios类型的数据结构中的值和使用一小 组函数调用,你就可以对终端接口进行控制. 可以被调整来影 ...
- rails 运维常用命令
创建生产环境数据库并执行迁移 $ RAILS_ENV=production rake db:create$ RAILS_ENV=production rake db:migrate RAILS_ENV ...
- PHP新写的大转盘抽奖源码
中奖概率 抽奖大转盘演示:http://www.sucaihuo.com/php/3301.html function getRand($proArr, $proCount) { $result = ...
- case 练习
#!/bin/bash RED_COLOR="\E[1;31m" GREEN_COLOR="\E[1;32m" YELLOW_COLOR="\E[1; ...
- 【转】RMQ-ST算法详解
地址:http://blog.csdn.net/z287438743z/article/details/8132806 RMQ(Range Minimum/Maximum Query)问题就是求区间最 ...