POJ 2262 Goldbach's Conjecture

Problem 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

用打表法

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int sum(int x){
    int i;
     int t = sqrt ( x + 0.5 ) ;
    for(i=2;i<=t;i++){
        if(x%i==0)
            return 0;
    }
    return 1;
}
int arr[500005],j=0;
void a(){
       int i;
    for(i=3;i<500000;i++){
        if(sum(i))
            arr[j++]=i;
    }
}
int main(){

    int m,i;
    a();
    /**for(i=0;i<10;i++)
    {
        printf("%6d",arr[i]);
    }**/
    while(1){
        int m;
        scanf("%d",&m);
    if(m==0){
        return 0;
    }
    if(m==6){
        printf("6 = 3 + 3\n");
    }
    else{
        int flag=0;
        for(i=0;i<m/2&&m/2>arr[i];i++)
        if(sum(m-arr[i])==1){
                flag=1;
                //printf("i=%d\n",i);
            break;
        }
       if(flag){
            //printf("##i=%d\n",i);
           printf("%d = %d + %d\n",m,arr[i],m-arr[i]);
       }
       else
          printf("Goldbach's conjecture is wrong.\n");
    }
    }
    return 0;
}
时间: 2024-12-19 20:46:55

POJ 2262 Goldbach&#39;s Conjecture的相关文章

poj 2262 Goldbach&amp;#39;s Conjecture 【素数筛】

这题必然的筛选法,出现了2个问题:1.开始开了一个 result 数组(全局变量),想把素数挨个存进来,虽然还计数估算了的,一直的runtime error , 后来发现是多此一举,去掉之后就变成wrong answer,看来可以编译了.这么说来,一百万对于两个大数组还是有点吃不消的  2.筛的时候一定要筛完整,for(j=2*i;j<MAXN;j+=i)prime[j]=1;这里开始用的 j<(MAXN/i),明显就错了. 另外我很好奇题目中说的"Goldbach's conjec

HDOJ 1397 Goldbach&amp;#39;s Conjecture(快速筛选素数法)

Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether

POJ 2262

 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. B

poj 1828 Monkeys&amp;#39; Pride 模拟

   排个序,模拟下就好了,水题一个 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algori

poj 3006 Dirichlet&amp;#39;s Theorem on Arithmetic Progressions 【素数筛】

说实话,题目很长,但是和真正要思考的东西关系不大... 就用了之前的素数筛的模板,控制了一下输入.输出格式就过了,很水的题,没什么技术含量,我好像也只会用暴搜... #include <stdio.h> #define MAXN 1000002 int prime[MAXN]; //用筛法求素数,1代表不是素数(被筛掉) int main() { //先打出素数表 prime[0]=prime[1]=1; //开始去掉prime[0]和prime[1] int i,j; for(i=2;i&l

poj 2081 Recaman&amp;#39;s Sequence【hash】

题目意思不难理解就是第m个位置的数是根据第m-1位置的数推出来的如果a[m-1]-m>0,并且a[m-1]-m在前面的序列中没有出现过那么a[m] = a[m-1]-m否则a[m] = a[m-1]+m 另外唯一需要注意的一点就是hash数组开大一点. //打表 #include <stdio.h> #include <iostream> using namespace std; const int MAXN=500003; int a[MAXN]={0}; bool has

poj 1658 Eva&amp;#39;s Problem

确实是非常水的题,在这里留个痕迹 AC的代码: #include<stdio.h> int main() { int n; scanf("%d",&n); int a[6],i; int gap; int result; while(n--) { for (i=1;i<=4;i++) scanf("%d",&a[i]); //等差更容易判断,如果不是等差就一定是等比 gap=a[2]-a[1]; if (gap==a[3]-a[2]

UVa 686 Goldbach&#039;s Conjecture (II):哥德巴赫猜想

686 - Goldbach's Conjecture (II) Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=627 Goldbach's Conjecture: For any even number n greater than or equal to

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford