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

 
View Code
第一种方法:筛选法,打素数表。
这种方法可以找出一定范围内的所有的素数。

思路是,要求10000以内的所有素数,把1-10000这些数都列出来,1不是素数,划掉;2是素数,所有2的倍数都不是素数,划掉;取出下一个幸存的数,划掉它的所有倍数;直到所有幸存的数的倍数都被坏掉为止。要找出10000以为的所有的素数,则需要一个大小为10000的数组,将其所有元素设置为未标记首先把1设置为标记,从2开始,标记所有是它倍数的数,然后对下一个没有标记的数进行标记它的倍数。当标记完成后,所有未标记的数即为素数。

code:

#include<stdio.h>
int prime[1000000];
int main()
{
int i,j,n,flag;
prime[2]=1;
for(i=3;i<1000000;i+=2)
prime[i]=1;
for(i=2;i<500000;i++)
{
if(prime[i])
for(j=1;j<1000000/i;j++)
prime[i+i*j]=0;
}
while(scanf("%d",&n),n)
{
flag=1;
for(i=3;i<n/2+1;i+=2)
if(prime[i]&&prime[n-i])
{
flag=0;
break;
}
if(flag)
printf("Goldbach's conjecture is wrong.");
else
printf("%d = %d + %d\n",n,i,n-i);

}
return 0;
}
View Code
第二种方法:

素数判断法:

这种方法是对上面方法的改进,上面方法是对2-sqrt(n)之间的数进行判断是否能除尽,而因为有如下算术基本定理,可以减少判断量。

算术基本定理:又称为素数的唯一分解定理,即:每个大于1的自然数均可写为素数的积,而且这些素因子按大小排列之后,写法仅有一种方式。例如:6936 = 2^3×3×17^21200 = 2^4×3×5^2

由算术基本定理知,任何合数都可分解为一些素数的乘积,所以判断一个数能不能被2-sqrt(n)之间的素数整除即可。但是必须知道2-sqrt(n)之间的所有素数。

code:



#include<stdio.h>
#include<string.h>
int prime[1000000];
int prime1000[1000];
int p;
int isprime1(int n)
{
int i;
if(n%2==0&&n!=2)
return 0;
if(n%3==0&&n!=3)
return 0;
for(i=2;i*i<=n;i++)
if(n%i==0)
return 0;
return 1;
}
int isprime2(int n)
{
int i;
for(i=0;i<p;i++)
if(n%prime1000[i]==0)
return 0;
return 1;
}
int main()
{
int i,n,j,flag;
for(i=2,p=0;i<1000;i++)
if(isprime1(i))
{
prime[i]=1;
prime1000[p++]=i;
}
prime[2]=0;
for(i=1000;i<1000000;i++)
if(isprime2(i))
prime[i]=1;
while(scanf("%d",&n),n)
{
flag=1;
for(i=3;i<n/2+1;i+=2)
if(prime[i]&&prime[n-i])
{
flag=0;
printf("%d = %d + %d\n",n,i,n-i);
break;
}
if(flag)
printf("Goldbach's conjecture is wrong.\n");

}
return 0;
}
View Code
第三种方法:

1、试除法

用n除以2-sqrt(n),有一个能除尽就不是素数,否则是素数。

时间复杂度:O(sqrt(n))

(此题没有打表尽然比打表还要快,看来表不是任何时候打都有好处的--!)

code:

#include<stdio.h>
int isprime(int n)
{
int i;
if(n%2==0&&n!=2)
return 0;
if(n%3==0&&n!=3)
return 0;
for(i=2;i*i<=n;i++)
if(n%i==0)
return 0;
return 1;
}
int main()
{
int n,i,flag;
while(scanf("%d",&n),n)
{
flag=1;
for(i=3;i<n/2+1;i+=2)
{
if(isprime(i)&&isprime(n-i))
{
flag=0;
printf("%d = %d + %d\n",n,i,n-i);
break;
}
}
if(flag)
printf("Goldbach's conjecture is wrong.\n");
}
return 0;
}




posted @ 2012-04-06 16:55  'wind  阅读(310)  评论(0编辑  收藏  举报