使用eratosthenes筛的Java goldbach猜想

时间:2015-10-28 00:55:36

标签: java arrays

我正在尝试创建一个程序,您输入一个数字,它会告诉您加上该数字的素数对。

这是我的代码:

import java.util.Scanner;

public class Goldbach
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input an integer to find two prime numbers whose sum add up to it.");
    int input = scan.nextInt();
    if(input < 4)
    {
      System.out.println("Number must be greater than or equal to 4");
    }
    long startTime = System.nanoTime();
    int[] primes = generatePrimes(input);    
    findPairs(input, primes);
    long endTime = System.nanoTime();
    System.out.println("\n This program took: " + (endTime-startTime) + " nanoseconds or " + ((endTime-startTime)*1e-9) + " seconds.");
  }

  private static int[] generatePrimes(int max)
  {
    boolean[] isComposite = new boolean[max + 1];
    for (int i = 2; i * i <= max; i++)
    {
      if (!isComposite [i])
      {
        for (int j = i; i * j <= max; j++)
        {
          isComposite [i*j] = true;
        }
      }
    }
    int numPrimes = 0;
    for (int i = 2; i <= max; i++)
    {
      if (!isComposite [i]) numPrimes++;
    }
    int [] primes = new int [numPrimes];
    int index = 0;
    for (int i = 2; i <= max; i++)
    {
      if (!isComposite [i]) primes [index++] = i;
    }
    return primes; 
  }

  private static void findPairs(int input, int[] primes)
  { 
    String primepairs = "";
    for(int i = 1; i < primes.length; i++)
    {
      for(int j = 1; j < primes.length -1; j++)
      {
        if((j > i) && ((j+i) == input))
        {
          primepairs = (primepairs == null ? "" : primepairs + "\n") + j + " + " + i + " = " + input;
        }
      }
    }
    System.out.println("\n" + primepairs);
  }
}

我已经尝试过调试但是我还没有成功。你能帮我调试吗?谢谢你指出我正确的方向

最终守则:

 /* Title: Goldbach Conjecture
 * Author: Harris R
 */

import java.util.Scanner;

public class Goldbach
{
  public static void main(String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.println("Please input an integer to find two prime numbers whose sum add up to it.");
    int input = scan.nextInt();
    if(input < 4)
    {
      System.out.println("Number must be greater than or equal to 4");
      return;
    }
    if(input%2 > 0)
    {
      System.out.println("Number must be even.");
      return;
    }
    long startTime = System.nanoTime();
    int[] primes = generatePrimes(input);    
    findPairs(input, primes);
    long endTime = System.nanoTime();
    System.out.println("\n This program took: " + (endTime-startTime) + " nanoseconds or " + ((endTime-startTime)*1e-9) + " seconds.");
  }

  private static int[] generatePrimes(int max)
  {
    boolean[] isComposite = new boolean[max + 1];
    for (int i = 2; i * i <= max; i++)
    {
      if (!isComposite [i])
      {
        for (int j = i; i * j <= max; j++)
        {
          isComposite [i*j] = true;
        }
      }
    }
    int numPrimes = 0;
    for (int i = 2; i <= max; i++)
    {
      if (!isComposite [i]) numPrimes++;
    }
    int [] primes = new int [numPrimes];
    int index = 0;
    for (int i = 2; i <= max; i++)
    {
      if (!isComposite [i]) primes [index++] = i;
    }
    return primes; 
  }

  private static void findPairs(int input, int[] primes)
  { 
    String primepairs = "";
    for(int i = 0; i < primes.length; i++)
    {
      for(int j = i; j < primes.length; j++)
        {
            int p = primes[i];
            int q = primes[j];
            if(p+q == input)
            {
                primepairs += "\n" + q + " + " + p + " = " + input;
            }
        }
      System.out.println("Program Completion: " + (((double)i/primes.length)*100) + "%");
    }
    System.out.println(primepairs);
  }
}

1 个答案:

答案 0 :(得分:0)

您的generatePrimes方法似乎没问题。

由于您要添加数组索引findPairsi而不是素数jprimes[i],因此primes[j]方法没有多大意义那些指数。

这是更正后的版本

private static void findPairs(int input, int[] primes)
{
    String primepairs = "";
    for(int i = 0; i < primes.length; i++)
    {
        for(int j = i; j < primes.length; j++)
        {
            int p = primes[i];
            int q = primes[j];
            if(p+q == input)
            {
                primepairs += "\n" + q + " + " + p + " = " + input;
            }
        }
    }
    System.out.println("\n" + primepairs);
}