[关闭]
@Chilling 2016-08-11T07:28:23.000000Z 字数 2240 阅读 1016

POJ-2262: Goldbach'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 = 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

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

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
0

Sample Output

8 = 3 + 5
20 = 3 + 17
42 = 5 + 37

题意:验证哥德巴赫猜想,任意一个大于4的偶数都可以写成两个素数相加。如果不止一种,输出b-a的差值最大的一组。

下面用两种筛法写了一下,其实并没有什么区别= =


埃式筛法

先判断1000000内的所有素数,从3开始循环,找到一个数i是素数并且n-i也是素数,就终止并按照要求输出。

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #define maxn 1000005
  5. bool isprime[maxn]; //是否素数
  6. void primeform() //nlogn
  7. {
  8. memset(isprime,true,sizeof(isprime));
  9. int m=(int)sqrt(maxn+0.5)+1;
  10. for(int i=2;i<m;i++)
  11. {
  12. if(isprime[i])
  13. for(int j=i*2;j<=maxn;j+=i)
  14. isprime[j]=false;
  15. }
  16. }
  17. int main()
  18. {
  19. int n,i,flag;
  20. primeform();
  21. while(scanf("%d",&n),n)
  22. {
  23. flag=0;
  24. for(i=3;i<=n/2+1;i++)
  25. {
  26. if(isprime[i]&&isprime[n-i])
  27. {
  28. flag=1;
  29. printf("%d = %d + %d\n",n,i,n-i);
  30. break;
  31. }
  32. }
  33. if(flag==0)
  34. printf("Goldbach's conjecture is wrong.\n");
  35. }
  36. return 0;
  37. }

快速线性筛法

将素数保存在数组内,循环,如果n-prime[i]也是素数,终止,按要求输出。

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #define maxn 1000005
  5. bool isprime[maxn]; //是否素数
  6. int prime[maxn]; //素数表
  7. void primeform()
  8. {
  9. int cnt=0;
  10. memset(isprime,true,sizeof(isprime));
  11. for(int i=2;i<=maxn;i++)
  12. {
  13. if(isprime[i])
  14. prime[cnt++]=i;
  15. for(int j=0;(j<cnt&&i*prime[j]<=maxn);j++)
  16. {
  17. isprime[i*prime[j]]=false;
  18. if(i%prime[j]==0)
  19. break;
  20. }
  21. }
  22. }
  23. int main()
  24. {
  25. int n,i,flag;
  26. primeform();
  27. while(scanf("%d",&n),n)
  28. {
  29. flag=0;
  30. for(i=1;;i++)
  31. {
  32. if(isprime[n-prime[i]])
  33. {
  34. printf("%d = %d + %d\n",n,prime[i],n-prime[i]);
  35. flag=1;
  36. break;
  37. }
  38. }
  39. if(flag==0)
  40. printf("Goldbach's conjecture is wrong.\n");
  41. }
  42. return 0;
  43. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注