POJ 2262-Goldbach's Conjecture(素数筛)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39435 | Accepted: 15119 |
Description
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
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
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> using namespace std; typedef long long LL; int prime[1000010]={2,3,5}; int k=3; int ip[1000010]={0}; void is_prime() { int i,j; int flag=0; int gad=2; ip[2]=1;ip[3]=1;ip[5]=1; for(i=7;i<=1000010;i+=gad){ flag=0; gad=6-gad; for(j=0;prime[j]*prime[j]<=i;j++){ if(i%prime[j]==0){ flag=1; break; } } if(!flag){ prime[k++]=i; ip[i]=1; } } } int main() { int n,i,j; is_prime(); while(~scanf("%d",&n)){ if(!n) break; int f=0; for(i=0;i<=k;i++){ j=n-prime[i]; if(ip[j]){ f=1; break; } } if(!f) printf("Goldbach's conjecture is wrong.\n"); else printf("%d = %d + %d\n",n,prime[i],j); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。