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. Evaluation of Expression Tree

    Evaluation of Expression Tree Given a simple expression tree, consisting of basic binary operators i ...

  2. gitlab ce 中删除空项目之后,没有删除掉,访问500

    在VirtualBox中的gitlab ce,在管理页面的操作如下: 新建一个仓库名为test的仓库,并从gitlab中导入 导入失败,使用root用户登录,在 Admin Area -> Pr ...

  3. C# Distinct方法的使用笔记

    引自:http://blog.csdn.net/shaopengfei/article/details/36426763 从C# 3.0开始提供了Distinct方法,这对于集合的使用有了更为丰富的方 ...

  4. 减小iOS应用程序的大小

    减小iOS应用程序的大小 本文译自:Reducing the size of my App Q: 怎样才能让我的程序安装包小一点,让程序的下载和安装更快速? A: 本文收集了一些减小程序安装包大小的相 ...

  5. 自动恢复被挂掉的hbase region server

    最近老遇到hbase的regionserver自动挂掉的情况 日志总是看不出什么原因 不得已弄了个定时任务,任务的脚本类似是: pssh -H "bluejoe1 bluejoe2...&q ...

  6. javascript应用:页面解析list和map封装后的json数据

    开发web项目时,经常会使用到的页面脚本语言javascript,使用它能让页面展示上的效果更多彩. 今天就来说一下,从数据库中获取到数据后在页面上的展示方式: 1.数据库取出数据放入list< ...

  7. VR开发中性能问题—OculusWaitForGPU

    http://blog.csdn.net/cartzhang/article/details/50788894 VR开发中性能问题-OculusWaitForGPU 本文章由cartzhang编写,转 ...

  8. 关于在DWZ使用ssh后台查询的几种方式

    1.查询所有记录,并以列表形式查询 DaoImpl public PageBean queryByPage(String hql, List<Object> listobj, int pa ...

  9. 【转】Linux网络编程入门

    (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...

  10. SOCKET编程:为什么recv不阻塞

    服务器端: #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #incl ...