POJ 2909 Goldbach's Conjecture

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10333   Accepted: 6118

Description

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. There can be many such numbers. 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 215. 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

CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 50000 + 10

using namespace std;

int n, pri[MAX_N], ans = 0;
bool check[MAX_N];

void Prime(){
    memset(check, 0, sizeof(check));
    int tot = 0; check[1] = 1;
    REP(i, 2, 32770){
        if(!check[i]) pri[++ tot] = i;
        REP(j, 1, tot){
            if(i * pri[j] > 32770) break; check[i * pri[j]] = 1;
            if(i % pri[j] == 0) break;
        }
    }
}

int main(){
    Prime();
    while(scanf("%d", &n) != EOF){
        if(n == 0) break;
        ans = 0;
        REP(i, 1, n / 2){
            if(!check[i] && !check[n - i]) ans ++;
        }
        printf("%d\n", ans);
    }
    return 0;
}            
 

POJ 2909 Goldbach's Conjecture

时间: 2024-11-09 00:57:43

POJ 2909 Goldbach's Conjecture的相关文章

POJ 2909 Goldbach&#39;s Conjecture(简单题)

[题意简述]:输入一个数,输出有几对素数对可以使他们的和正好等于这个数 [分析]:暴力打表,再暴力循环求解 //268K 125Ms #include<iostream> using namespace std; #define N 35000 // 2^15 bool isprime[N]; int prime[N],nprime;//prime[N]用来存储素数,nprime是此时一共有多少素数 void doprime(int n) { int i,j; nprime = 1; mems

poj 2262 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39353   Accepted: 15077 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture (求解素数的一般筛和线性筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40944   Accepted: 15664 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture 数学常识 难度:0

题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分析: 在1e6内约有8e4个奇质数,因为a <= b,时间复杂度在T*4e4+1e6等级.一般T为1e3,足以承受 空间复杂度为1e6,足以承受 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm&g

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

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.如果存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的所有素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,如果存在素数a+素数b

POJ 2262 Goldbach&#39;s Conjecture(素数筛选法)

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 =

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

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.假设存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的全部素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,假设存在素数a+素数b

Poj 2262 / OpenJudge 2262 Goldbach&#39;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 a