POJ 2262 Goldbach's Conjecture (素数判断)

Goldbach‘s Conjecture

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37693   Accepted: 14484

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

Source

Ulm Local 1998

直接从较大的数开始枚举,然后判断两个数是不是素数

用时有点多

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 using namespace std;
 7 int isprime(int num)
 8 {
 9     int k=sqrt(num),i;
10     for(i=2;i<=k;i++)
11     {
12         if(num%i==0)
13             break;
14     }
15     if(i>k)
16         return 1;
17     else
18         return 0;
19 }
20 int main()
21 {
22     //freopen("in.txt","r",stdin);
23     int n;
24     while(scanf("%d",&n)&&n)
25     {
26         int flag=0,ans;
27         for(int i=n-2;i>=0;i--)
28         {
29             if(isprime(i))
30             {
31                 ans=n-i;
32                 if(isprime(ans))
33                 {
34                     flag=1;
35                     printf("%d = %d + %d\n",n,ans,i);
36                     break;
37                 }
38             }
39         }
40         if(flag==0)
41             printf("Goldbach‘s conjecture is wrong.\n");
42     }
43     return 0;
44 }

不知道为什么,先把素数筛出来再拿出来用会超时

(TLE)

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 using namespace std;
 7 const int MAXN=1000000+10;
 8 const int N=999983;
 9 int prime[MAXN],vis[MAXN];
10 int cnt;
11 void init()//素数筛法
12 {
13     int i,j;
14     for(i=2;i<=N;i++)
15     {
16         if(i%2==0)
17             vis[i]=0;
18         else
19             vis[i]=1;
20     }
21     for(i=3;i<=sqrt(N);i+=2)
22     {
23         if(vis[i])
24             for(j=i+i;j<N;j+=i)
25                 vis[j]=0;
26     }
27     cnt=1;
28     prime[0]=2;
29     for(i=2;i<N;i++)
30         if(vis[i])
31             prime[cnt++]=i;
32 }
33 int main()
34 {
35     //freopen("in.txt","r",stdin);
36     init();
37     int n;
38     while(scanf("%d",&n)&&n)
39     {
40         int flag=0,ans;
41         for(int i=cnt-1;i>=0;i--)
42         {
43             if(prime[i]<n)
44             {
45                 ans=n-prime[i];
46                 if(binary_search(prime,prime+cnt,ans))
47                 {
48                     printf("%d = %d + %d\n",n,ans,prime[i]);
49                     flag=1;
50                     break;
51                 }
52             }
53         }
54         if(flag==0)
55             printf("Goldbach‘s conjecture is wrong.\n");
56     }
57     return 0;
58 }

POJ 2262 Goldbach's Conjecture (素数判断),布布扣,bubuko.com

POJ 2262 Goldbach's Conjecture (素数判断)

时间: 2024-12-18 02:39:58

POJ 2262 Goldbach's Conjecture (素数判断)的相关文章

POJ 2262 Goldbach&#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.如果存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的所有素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,如果存在素数a+素数b

POJ 2262 Goldbach&#39;s Conjecture (求解素数的一般筛和线性筛)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40944   Accepted: 15664 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;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 =

poj 2262 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39353   Accepted: 15077 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

POJ 2262 Goldbach&#39;s Conjecture 数学常识 难度:0

题目链接:http://poj.org/problem?id=2262 哥德巴赫猜想肯定是正确的 思路: 筛出n范围内的所有奇质数,对每组数据试过一遍即可, 为满足b-a取最大,a取最小 时空复杂度分析: 在1e6内约有8e4个奇质数,因为a <= b,时间复杂度在T*4e4+1e6等级.一般T为1e3,足以承受 空间复杂度为1e6,足以承受 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm&g

POJ 2262 Goldbach&amp;#39;s Conjecture(素数相关)

POJ 2262 Goldbach's Conjecture(素数相关) http://poj.org/problem?id=2262 题意: 给你一个[6,1000000]范围内的偶数,要你将它表示成两个素数相加和的形式.假设存在多组解,请输出两个素数差值最大的解. 分析: 首先我们用素数筛选法求出100W以内的全部素数. 筛选法求素数可见: http://blog.csdn.net/u013480600/article/details/41120083 对于给定的数X,假设存在素数a+素数b

Poj 2262 / OpenJudge 2262 Goldbach&#39;s Conjecture

1.Link: http://poj.org/problem?id=2262 http://bailian.openjudge.cn/practice/2262 2.Content: Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37791   Accepted: 14536 Description In 1742, Christian Goldbach, a German a

POJ 2909 Goldbach&#39;s Conjecture(简单题)

[题意简述]:输入一个数,输出有几对素数对可以使他们的和正好等于这个数 [分析]:暴力打表,再暴力循环求解 //268K 125Ms #include<iostream> using namespace std; #define N 35000 // 2^15 bool isprime[N]; int prime[N],nprime;//prime[N]用来存储素数,nprime是此时一共有多少素数 void doprime(int n) { int i,j; nprime = 1; mems

POJ 2909 Goldbach&#39;s Conjecture

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10333   Accepted: 6118 Description For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2 This conjecture has not