poj 2909 Goldbach's Conjecture
2011-11-22 13:53
190 查看
Goldbach's Conjecture
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8274 | Accepted: 4779 |
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
#include<iostream> #include<cmath> using namespace std; const int maxsize=32768; int prime[maxsize]; int isPrime(int n) { int i; if(n!=2&&n%2==0) return 0; for(i=3;i<=sqrt(n*1.0);i+=2) { if(n%i==0) return 0; } return 1; } void judge() { int i,j; for(i=2,j=0;i<=33000;i++) { if(isPrime(i)==1) { prime[j]=i; j++; } } } int main() { int n; int i,j; int count; judge(); while(1) { cin>>n; if(n==0) break; count=0; for(i=0;i<maxsize;i++) { for(j=i;j<maxsize;j++) { if(prime[i]+prime[j]==n) count++; if(prime[i]+prime[j]>n) break; } if(prime[i]*2>n) break; } cout<<count<<endl; } return 0; }
相关文章推荐
- 筛素数——POJ 2909 Goldbach's Conjecture
- poj-2909-哥德巴赫猜想
- Poj 2662,2909 Goldbach's Conjecture (素数判定)
- poj 2909 哥德巴赫猜想
- POJ 2909 Goldbach's Conjecture (求x=p1+p2)
- poj 2909
- Poj 2662,2909 Goldbach's Conjecture (素数判定)
- POJ 2909 Goldbach's Conjecture
- poj2909 欧拉素数筛选
- poj 2909 Goldbach's Conjecture
- POJ 2909 哥德巴赫猜想
- Cpp环境 【poj 2940 】【Uva11054】【Vijos2909】Wine Trading in Gergovia 格尔高维亚的肮脏红酒交易
- poj2909 || poj2262
- POJ-2909-Goldbach's Conjecture
- POJ2909_Goldbach's Conjecture【素数判断】【水题】
- POJ 2909 Goldbach's Conjecture(简单题)
- poj 2909 Goldbach's Conjecture
- POJ 2909 && HDU 1397 Goldbach's Conjecture(数论)
- POJ 2909 Goldbach's Conjecture(素数表)
- POJ 2909 Goldbach's Conjecture(我的水题之路——任一数为素数对之和)