Poj 2262 / OpenJudge 2262 Goldbach's Conjecture
1.Link:
http://poj.org/problem?id=2262
http://bailian.openjudge.cn/practice/2262
2.Content:
Goldbach's Conjecture
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37791 Accepted: 14536 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
0Sample Output
8 = 3 + 5
20 = 3 + 17
42 = 5 + 37Source
3.Method:
筛素数法
4.Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring> #define MAX_NUM 1000000 using namespace std; int main()
{
//freopen("D://input.txt","r",stdin); int i,j; bool * arr_prime = new bool[MAX_NUM + ]; for(i = ; i <= MAX_NUM; i += ) arr_prime[i] = true;
for(i = ; i <= MAX_NUM; i += ) arr_prime[i] = false;
arr_prime[] = true; int sqrt_mn = sqrt(MAX_NUM);
for(i = ; i <= sqrt_mn; i += )
{
if(arr_prime[i])
{
for(j = i + i; j <= MAX_NUM; j += i) arr_prime[j] = false;
}
} int a;
cin >> a; while(a != )
{ if(a % == && arr_prime[a - ])
{
cout << a << " = " << "" << " + " << (a - ) << endl;
}
else
{
for(i = ; i <= a / ; i += )
{
if(arr_prime[i] && arr_prime[a - i])
{
cout << a << " = " << i << " + " << (a - i) << endl;
break;
}
}
} cin >> a;
} return ;
}
5.Reference:
http://blog.csdn.net/liukehua123/article/details/5482854
Poj 2262 / OpenJudge 2262 Goldbach's Conjecture的更多相关文章
- Poj 2662,2909 Goldbach's Conjecture (素数判定)
一.Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 2262 Goldbach's Conjecture——筛质数(水!)
题目:http://poj.org/problem?id=2262 大水题的筛质数. #include<iostream> #include<cstdio> #include& ...
- POJ 2262 Goldbach's Conjecture (打表)
题目链接: https://cn.vjudge.net/problem/POJ-2262 题目描述: In 1742, Christian Goldbach, a German amateur mat ...
- [暑假集训--数论]poj2262 Goldbach's Conjecture
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...
- Goldbach’s Conjecture(信息学奥赛一本通 1622)
[题目描述] 原题来自:Ulm Local,题面详见:POJ 2262 哥德巴赫猜想:任何大于 44 的偶数都可以拆成两个奇素数之和. 比如: 8=3+5 20=3+17=7+13 42=5+37=1 ...
- Goldbach's Conjecture
Goldbach's Conjecture Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...
- Goldbach's Conjecture(哥德巴赫猜想)
Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
随机推荐
- 使用IIS 7.0 Smooth Streaming 优化视频服务
http://www.cnblogs.com/dudu/archive/2013/06/08/iis_webserver_settings.html (支持高并发的IIS Web服务器常用设置) ht ...
- codeigniter分页类传多个参数(转)
http://example.com/index.php/控制器/方法名/页面的偏移值 页面的偏移值必须是方法名后第一个参数,否者分页类不能判断当前是哪一页,而用ci的分页类进行页面跳转时他是把偏移值 ...
- 今天分享一个抽奖的类Lottery
/* * Copyright (C) 2014 Jason Fang ( ijasonfang@gmail.com ) * * Licensed under the Apache License, V ...
- java_redis3.0.3集群搭建
redis3.0版本之后支持Cluster,具体介绍redis集群我就不多说,了解请看redis中文简介. 首先,直接访问redis.io官网,下载redis.tar.gz,现在版本3.0.3,我下面 ...
- mysql颠覆实战笔记(三)-- 用户登录(二):保存用户操作日志的方法
版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...
- js常用代码收藏
--1.遍历string分割为数组 <script language="javascript"> str="2,2,3,5,6,6"; //这是一字 ...
- Linux下VirtualBox出现kernel driver not installed的解决方法
今天安装好rhel-server-6.6-i386后,再安装VirtualBox成功,但是再VirtualBox中创建虚拟机的时候出现了“不能为xx虚拟机打开新任务” 并弹出如下的错误信息:
- shareplex三点同步配置
一.准备工作 主从类型 系统版本 数据库版本 主机地址 主机名 源数据库 Centos6.4 X86_64 11.2.0.4.0 192.168.3.230 dbshareplex 目的数据库 Cen ...
- [改善Java代码]静态变量一定要先声明后赋值
建议32: 静态变量一定要先声明后赋值 这标题看着让人很纳闷,什么叫做变量一定要先声明后赋值?Java中的变量不都是先声明后使用的吗?难道还能先使用后声明?能不能暂且不说,我们先来看一个例子,代码如下 ...
- Linux系统调用(转载)
目录: 1. Linux系统调用原理 2. 系统调用的实现 3. Linux系统调用分类及列表 4.系统调用.用户编程接口(API).系统命令和内核函数的关系 5. Linux系统调用实例 6. Li ...