http://poj.org/problem?id=2262

Goldbach's Conjecture
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34323   Accepted: 13169

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

Source

 
【题解】:
题意:输入一个偶数n,将n分成两个素数和,找到第一组就可以(n=a+b,a尽量小)
n最大1000000
方法:素数筛选法
【code】:
 /**
Judge Status:Accepted Memory:4852K
Time:344MS Language:G++
Code Lenght:797B Author:cj
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm> #define N 1000000
using namespace std; int prim[N+];
int arrPrim[N+]; int main()
{
int i,j;
memset(prim,,sizeof(prim));
for(i=;i*i<=N;i++) //素数筛选法
{
if(!prim[i])
{
for(j=;j*i<=N;j++)
{
prim[j*i]=;
}
}
}
int arr_cnt=;
for(i=;i<=N;i++)
{
if(!prim[i]) arrPrim[arr_cnt++]=i; //一次遍历赛选出来的素数存入数组,遍历用
}
int n;
while(~scanf("%d",&n)&&n)
{
for(i=;i<arr_cnt;i++)
{
if(!prim[n-arrPrim[i]])
{
printf("%d = %d + %d\n",n,arrPrim[i],n-arrPrim[i]);
break;
}
}
}
return ;
}

poj 2262 Goldbach's Conjecture(素数筛选法)的更多相关文章

  1. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

  2. poj 2262 Goldbach's Conjecture——筛质数(水!)

    题目:http://poj.org/problem?id=2262 大水题的筛质数. #include<iostream> #include<cstdio> #include& ...

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

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

  4. POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)

    http://poj.org/problem?id=2262 题意: 哥德巴赫猜想,把一个数用两个奇素数表示出来. 思路:先用Eratosthenes筛法打个素数表,之后枚举即可. #include& ...

  5. POJ 2262 Goldbach's Conjecture 数学常识 难度:0

    题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分 ...

  6. poj 2262 Goldbach's Conjecture

    素数判定...很简单= =.....只是因为训练题有,所以顺便更~ #include<cstdio> #include<memory.h> #define maxn 50000 ...

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

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

  8. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  9. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

随机推荐

  1. Java - 正则表达式常用操作

    验证 简单验证 String regex = "\\d{4}-\\d{2}-\\d{2}"; String input = "2016-01-01"; asse ...

  2. JMS - ExceptionListener

    If a JMS provider detects a problem with a connection, it will inform the connection’s ExceptionList ...

  3. IUSR和IIS_IUSRS

    在早期的IIS版本中,随着IIS的安装,系统会创建一个IUSR_MachineName用户.IIS启用匿名访问,就是通过此用户进行身份认证的,包括FTP匿名访问,HTTP匿名访问.   在创建IUSR ...

  4. 修改Windows硬盘分区名称

    本文由 www.169it.com 收集整理 如果用户在将 XP 重装成Win7/Win8时,原本的硬盘分区名称可能会出现无法更改的情况,重新命名也都起不了作用.这种情况一般是因为使用 XP 系统下 ...

  5. 第二十九篇、CoreAnimation的使用

    使用的的三个步骤 1.初始化演员 2.设置好剧情 3.播放 主要类: CALayer // 绘图部分 CABaseAnimation // 基本动画(缩放,移动) CAKeyframeAnimatio ...

  6. Cocos2d-x中创建SQLite数据库

    我们下边介绍如何通过SQLite3提供的API实现MyNotes数据库创建.创建数据库一般需要经过如下三个步骤.(1) 使用sqlite3_open函数打开数据库.(2) 使用sqlite3_exec ...

  7. 基于 HTML5 的数据存储

    以前想做个静态网页APP.最初的思路是用本地文件存储数据,后来发现在手机上运行时,文件无法找到. 经过了长达几个月的搜索(实际也就几天),没有找到合适的方法. 就在绝望的时候,无意间搜到基于HTML5 ...

  8. VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks Small-Footprint Configuration

    什么是Small-footprint? Small-footprint常见关键配置? 如何配置Small-footprint? 什么是Small-footprint? Small-footprint配 ...

  9. AngularJS 的那些内置九种过滤器

    ng内置了九种过滤 1. currency (货币处理) 使用currency可以将数字格式化为货币,默认是美元符号,你可以自己传入所需的符号,例如我传入人民币: {{num | currency : ...

  10. Windows下Android模拟环境的搭建