2个数的和为素数

时间:2018-10-20 17:16:28

标签: java

我正在研究一个给定数字为4的程序,我需要找出使用从1到4的所有数字的可能方式,以使彼此相邻的数字加起来为质数

例如,如果给定数字为4,则可能的方法是:

1 2 3 4
1 4 3 2

我正在使用以下方法,请告诉我是否有任何简化方法:

第1步:找到数字1到4的所有可能组合,例如

1 2 3 4
1 3 2 4
1 3 4 2
2 3 4 1
2 3 1 4
etc

第2步:找出符合给定要求的序列,然后增加一个计数器。最后显示计数器的值。

有更好的方法吗?

2 个答案:

答案 0 :(得分:1)

这是一个使用回溯来加快程序速度的解决方案。

public class PrimeSum {

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

  private static void findSolutions(int[] a, int n) {
    if(n >= a.length) {
      System.out.println(java.util.Arrays.toString(a));
    } else {
      for(int i = n; i < a.length; i++) {
        if(n == 0 || isPrime(a[n - 1] + a[i])) {
          int t = a[n];
          a[n] = a[i];
          a[i] = t;
          findSolutions(a, n + 1);
          t = a[n];
          a[n] = a[i];
          a[i] = t;
        }
      }
    }
  }

  public static void main(String[] args) {
    int[] a = new int[4];
    for(int i = 0; i < a.length; i++) {
      a[i] = i + 1;
    }
    findSolutions(a, 0);
  }
}

答案 1 :(得分:0)

我采取了一些不同的方法。首先,我看一下允许哪些对。因此,以数字4开头时,您可以使用以下选项:

  • 将1与2或4组合
  • 将2与1或3组合
  • 将3与2或4结合在一起
  • 将4与1或3组合

您现在需要做的是,从两个数字的潜在序列开始:

  • 1,2
  • 1,4
  • 2,1
  • 2,3
  • 3,2
  • 3,4
  • 4,1
  • 4,3

之后,您就可以开始扩展它们了。因此,对于第一个(1,2),您可以选择添加1或3,对于2,可用选项为1或3(请参见第一个项目符号列表)。给出序列1,2,1和1,2,3。由于您不能两次使用数字,因此可以忽略第一个选项。

这样,您将得到以下三个数字的序列:

  • 1,2,3
  • 1、4、3
  • 2,1,4
  • 2,3,4
  • 3,2,1
  • 3,2,4
  • 3,4,1
  • 4,1,2
  • 4、3、2

继续执行此操作,您将得到解决方案:

  • 1,2,3,4
  • 1、4、3、2
  • 2,1,4,3
  • 2,3,4,1
  • 3,2,1,4
  • 3,4,1,2
  • 4,1,2,3
  • 4,3,2,1

这是我在代码中的解决方案:

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Scanner;


public class Main2 {
    static Scanner sc = new Scanner(System.in);

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

    public static void main(String[] args) {

        //Get number from user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a value");
        int number = scanner.nextInt();

        LinkedList<LinkedList<Integer>> solutions = new LinkedList<>();

        //Make a HashMap with all the viable combinations using two numbers
        //Make a HashMap with all potential combinations
        HashMap<Integer, LinkedList<Integer>> viableCombinations = new HashMap<>();
        LinkedList<LinkedList<Integer>> potentialSolutions = new LinkedList<>();
        for (int i = 1; i <= number; i++) {
            for (int j = i + 1; j <= number; j++) {
                if (isPrime(i+j)) {
                    if (viableCombinations.containsKey(i)) {
                        viableCombinations.get(i).add(j);
                    }
                    else {
                        LinkedList<Integer> newCombination = new LinkedList<Integer>();
                        newCombination.add(j);
                        viableCombinations.put(i, newCombination);
                    }
                    if (viableCombinations.containsKey(j)) {
                        viableCombinations.get(j).add(i);
                    }
                    else {
                        LinkedList<Integer> newCombination = new LinkedList<Integer>();
                        newCombination.add(i);
                        viableCombinations.put(j, newCombination);
                    }

                    LinkedList<Integer> combination = new LinkedList<>();
                    combination.add(i);
                    combination.add(j);
                    potentialSolutions.add(combination);
                    combination = new LinkedList<>();
                    combination.add(j);
                    combination.add(i);
                    potentialSolutions.add(combination);
                }
            }
        }

        //Extend HashMap with all viable combinations
        while (potentialSolutions.size() > 0) {
            LinkedList<Integer> potentialSolution = potentialSolutions.pop();
            if (potentialSolution.size() == number) {
                solutions.add(potentialSolution);
            }
            else {
                LinkedList<Integer> combinations = viableCombinations.get(potentialSolution.getLast());
                for (int i = 0; i < combinations.size(); i++) {
                    LinkedList<Integer> newPotentialSolution = new LinkedList<>(potentialSolution);
                    if (!newPotentialSolution.contains(combinations.get(i))) {
                        newPotentialSolution.add(combinations.get(i));
                        potentialSolutions.add(newPotentialSolution);
                    }
                }
            }

        }
        System.out.println(solutions);
    }
}
相关问题