poj 2262 Goldbach's Conjecture【质数和】

本文介绍了一种验证哥德巴赫猜想的算法实现,该猜想指出所有大于4的偶数都可以表示为两个奇质数之和。文章提供了三种不同的算法实现方案,包括筛选法、素数判断法和试除法。
部署运行你感兴趣的模型镜像

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;
}




转载于:https://www.cnblogs.com/dream-wind/archive/2012/04/06/2434929.html

您可能感兴趣的与本文相关的镜像

Anything-LLM

Anything-LLM

AI应用

AnythingLLM是一个全栈应用程序,可以使用商用或开源的LLM/嵌入器/语义向量数据库模型,帮助用户在本地或云端搭建个性化的聊天机器人系统,且无需复杂设置

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值