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

 
 
直接从较大的数开始枚举,然后判断两个数是不是素数
用时有点多
 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,ans,i);
36                     break;
37                 }
38             }
39         }
40         if(flag==0)
41             printf("Goldbach's conjecture is wrong.
");
42     }
43     return 0;
44 }
View Code

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

(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,ans,prime[i]);
49                     flag=1;
50                     break;
51                 }
52             }
53         }
54         if(flag==0)
55             printf("Goldbach's conjecture is wrong.
");
56     }
57     return 0;
58 }
View Code
原文地址:https://www.cnblogs.com/clliff/p/3923614.html