HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)【水题+打表】

708 篇文章 19 订阅
509 篇文章 9 订阅

Goldbach's Conjecture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7178    Accepted Submission(s): 2788


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 this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.
 

Input
An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.
 

Output
Each output line should contain an integer number. No other characters should appear in the output.
 

Sample Input
  
  
6 10 12 0
 

Sample Output
  
  
1 2 1
 

Source


Regionals 1998 >> Asia - Tokyo


问题链接HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)

问题简述:参见上文。

问题分析

  这个问题是验证哥德巴赫猜想,对于输入的n,计算和等于n的奇素数对的个数。

  不预先打表时间上会超时。

程序说明

  这个问题,HDU1397和UVA686的测试数据是不一样的。最初做的程序,HDU中AC,而UVA中WA。估计原因是UVA868中的n是包含奇数的,例如7=2+5。所以后来写的程序将计算素数对的循环做了修改,程序通过了。

  这里分别给出C语言程序和C++语言程序的两个版本。


AC的C语言程序如下

/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */

#include <stdio.h>
#include <math.h>

#define MAXN 32767

int prime[MAXN+2] = {0, 0, 1, 1, 0};

// 试除法判断一个数是否为素数
int isprime(int n)
{
    int end2, i;

    end2 = sqrt(n);
    for(i=3; i<=end2; i+=2) {
        if(n % i == 0)
            break;
    }

    return i > end2 ? 1 : 0;
}

void maketable(int n)
{
    int i;

    for(i=5; i<n; i+=2) {
        prime[i] = isprime(i);
        prime[i+1] = 0;
    }
}

int main(void)
{
    int n;
    int count, i;

    // 打表
    maketable(MAXN);

    while(scanf("%d", &n) != EOF) {
        // 判定结束条件
        if(n == 0)
            break;

        // 计算素数对个数
        count = 0;
        for(i=2; i<=n/2; i++)
            if(prime[i] && prime[n-i])
                count++;

        // 输出结果
        printf("%d\n", count);
    }

    return 0;
}

AC的C++语言程序如下:

/* HDU1397 POJ2909 UVA686 UVALive5674 Goldbach's Conjecture(II) */

#include <iostream>
#include <cmath>
#include <cstdio>

using namespace std;

const int MAXN = 2000000;
bool prime[MAXN+1] = {false, false, true};

// 埃氏筛选法
void esieve(bool sflag[], int n)
{
    // 初始化
    for(int i=3; i<=n; i++) {
        sflag[i++] = true;
        sflag[i] = false;
    }

    // 筛选
    int max = sqrt(n);
    for(int i=3; i<=max; i++) {
        if(sflag[i]) {
            for(int j=i+i; j <= n; j+=i)
                sflag[j] = false;
        }
    }
}

int main()
{
    esieve(prime, MAXN);

    int n, count, i;

    while(scanf("%d", &n) != EOF) {
        // 判定结束条件
        if(n == 0)
            break;

        // 计算素数对个数
        count = 0;
        for(i=2; i<=n/2; i++)
            if(prime[i] && prime[n-i])
                count++;

        // 输出结果
        printf("%d\n", count);
    }

    return 0;
}


HDU中AC,而UVA中WA的C语言程序如下:

/* HDU1397 POJ2909 UVA686 Goldbach's Conjecture(II) */

#include <stdio.h>
#include <math.h>

#define MAXN 32767

int prime[MAXN+2] = {0, 0, 1, 3, 0};

// 试除法判断一个数是否为素数
int isprime(int n)
{
    int end2, i;

    end2 = sqrt(n);
    for(i=3; i<=end2; i+=2) {
        if(n % i == 0)
            break;
    }

    return i > end2 ? 1 : 0;
}

void maketable(int n)
{
    int i;

    for(i=5; i<n; i+=2) {
        prime[i] = isprime(i);
        prime[i+1] = 0;
    }
}

int main(void)
{
    int n;
    int count, i;

    // 打表
    maketable(MAXN);

    while(scanf("%d", &n) != EOF) {
        // 判定结束条件
        if(n == 0)
            break;

        // 计算素数对个数
        count = 0;
        for(i=3; i<=n/2; i+=2)
            if(prime[i] && prime[n-i])
                count++;

        // 输出结果
        printf("%d\n", count);
    }

    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值