poj2262 Goldbach's Conjecture
用欧拉筛把素数筛出来,再枚举一下。
#include<iostream> #include<cstdio> #include<cstring> #define re register using namespace std; #define N 1000001 int n,v[N],pri[N],cct; bool is[N]; int main(){ for(int i=2;i<N;++i){ if(!v[i]) is[v[i]=pri[++cct]=i]=1; for(int j=1;j<=cct;++j){ if(pri[j]>i||pri[j]*i>=N) break; v[pri[j]*i]=pri[j]; } } while(scanf("%d",&n)){ if(!n) return 0; bool flag=1; for(int i=2;i<=cct&&flag;++i) if(is[i]&&is[n-i]) printf("%d = %d + %d\n",n,i,n-i),flag=0; if(flag) puts("Goldbach's conjecture is wrong."); }return 0; }