首页 > 专题系列 > Java解POJ > POJ 2262 Goldbach’s Conjecture [解题报告] Java
2013
11-11

POJ 2262 Goldbach’s Conjecture [解题报告] Java

Goldbach’s Conjecture

问题描述 :

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.

输入:

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.

输出:

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.”

样例输入:

8
20
42
0

样例输出:

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

解题代码:

解法(1)
import java.util.*;
 public class Main{
   public static void main(String args[]){
     Scanner sc=new Scanner(System.in);
      int n=1;
      while(true){
        n=sc.nextInt();
        if(n==0) break;
       test(n);
      }
      
   }
   /* 
   * 8 = 3 + 5 
   * 20 = 3 + 17
   * 42 = 5 + 37 
   */  
  public static void test(int x){ 
      if(6<=x && x < 1000000){ 
          if(x%2!=0){  
            System.out.println("输入数据不是偶数!");
           }else{
              boolean b=false;  
            for(int i=3;i+i<=x;i++){  
                if(isPrime(i) && isPrime(x-i)){ 
                    System.out.println(x+" = "+i+" + "+(x-i));  
                   b = true; 
                    break; 
                 }  
            }  
            if(!b)   
               System.out.println("Goldbach's conjecture is wrong."); 
          } 
      }else{ 
          System.out.println("输入数据范围不对");
       }  
  }   

   /* 
    * 判断某个数是否为质数,如果是返回true
    */

    public static boolean isPrime(int x){ 
      for(int i=2;i*i<=x;i++){ 
          if(x%i==0)
              return false;  
     }  
     return true; 
   }
}

解法(2)
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;

 public class Main {

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         List prime = new ArrayList();
         boolean[] b = new boolean[1000000];
         int s = 0;
         int a1 = 0;
         int a2 = 0;
         int max = 0;
         int min = 0;
         int mid = 0;
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         prime.add(2);
         loop: for (int i = 3; i <= 1000000; i += 2) {
             if (!b[i]) {
                 for (int j = 3; j <= Math.sqrt(i); j += 2) {
                     if (i % j == 0) {
                         continue loop;
                     }
                 }
                 prime.add(i);
                 for (int k = 2; i * k < 1000000; k++) {
                     b[i * k] = true;
                 }
             }
         }
         while ((s = Integer.parseInt(read.readLine())) != 0) {
             loop: for (int i = 0; i < prime.size(); i++) {
                 a1 = prime.get(i);
                 a2 = 0;
                 min = i;
                 max = prime.size() - 1;
                 while (max >= min) {
                     mid = (max + min) / 2;
                     if (a1 + prime.get(mid) == s) {
                         a2 = prime.get(mid);
                         break loop;
                     } else if (a1 + prime.get(mid) > s) {
                         max = mid - 1;
                     } else {
                         min = mid + 1;
                     }
                 }
             }
             if (a2 != 0) {
                 System.out.printf("%d = %d + %d\n", s, a1, a2);
             } else {
                 System.out.println("Goldbach's conjecture is wrong.");
             }
         }
     } 
}

  1. [email protected]

  2. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确