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,找有多少种方 ...
随机推荐
- 小程序JSON数组操作
- Linux安装Nginx使用反向代理
nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)1.实验环境准备准备2个服务器,都安装好nginx软件nginx1 192.168.13.79 作为web服务器 (理解为火车票售票点) ...
- android 半透明弹窗
<style name="edit_AlertDialog_style" parent="@android:style/Theme.Dialog"> ...
- windows环境下JDK1.8安装
jdk的安装,在Windows环境下没有什么特殊的选项,只需要“傻瓜式”安装就行(下一步). 如果说有的话,那就是你安装的路径,默认是c盘下的路径,可以根据自己的喜好,安装到自己的意愿目录: 当然,j ...
- 教你管理SQL数据库系列(1-4)
原文 教你管理 SQL Server 数据库(1)数据库的结构 http://bbs.51cto.com/thread-1084951-1.html教你管理 SQL Server 数据库(2)系统数 ...
- SQL性能优化常用语句(摘录网上)
1.把trace文件导入到表中 , ) AS RowNumber,* into TableName FROM fn_trace_gettable('trace.trc', default) 2.查询C ...
- pymysql插入datetime类型
第一种 create_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 第二种 update_time=time ...
- bzoj 2005: [Noi2010]能量采集 筛法||欧拉||莫比乌斯
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MB[Submit][Status][Discuss] Description 栋栋 ...
- AIM Tech Round 3 (Div. 2) A , B , C
A. Juicer time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...