POJ 2262 Goldbach's Conjecture (打表)
题目链接:
https://cn.vjudge.net/problem/POJ-2262
题目描述:
In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:
For example:
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
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.
The input will contain one or more test cases.
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
Each test case consists of one even integer n with 6 <= n < 1000000.
Input will be terminated by a value of 0 for n.
For each test case, print one line of the form n = a + b, where a
and b are odd primes. Numbers and operators should be separated by
exactly one blank like in the sample output below. If there is more than
one pair of odd primes adding up to n, choose the pair where the
difference b - a is maximized. If there is no such pair, print a line
saying "Goldbach's conjecture is wrong."
Sample Input
8 20 42 0Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
1 /* 2 题意描述 3 将一个数分成两个奇素数之和的形式,即c=a+b,如果有多种方案,输出b-a差值最大的 4 5 解题思路 6 打表,枚举查询 7 */ 8 #include<cstdio> 9 #include<cmath> 10 #include<cstring> 11 const int maxn=1000000+10; 12 bool isprim[maxn]; 13 void makeprim(int n); 14 15 int main() 16 { 17 makeprim(maxn); 18 int n,i; 19 //freopen("E:\\testin.txt","r",stdin); 20 while(scanf("%d",&n) == 1 && n != 0){ 21 for(i=3;i<=maxn;i++){ 22 if(i&1 && isprim[i] && (n-i)&1 &&isprim[n-i]){ 23 printf("%d = %d + %d\n",n,i,n-i); 24 break; 25 } 26 } 27 if(i == maxn+1) 28 printf("Goldbach's conjecture is wrong.\n"); 29 } 30 return 0; 31 } 32 33 void makeprim(int n){ 34 memset(isprim,1,sizeof(isprim)); 35 isprim[0]=isprim[1]=0; 36 int k=(int)sqrt(n+0.5); 37 for(int i=2;i<=k;i++){ 38 if(isprim[i]){ 39 for(int j=i*2;j<=n;j+=i) 40 isprim[j]=0; 41 } 42 } 43 }
欢迎交流,共同进步——