日期:2014-05-20 浏览次数:20724 次
public class Test { public static void main(String[] args) { int[] array = {2, 3, 4, 5,6}; System.out.println(test(array)); System.out.println(); array = new int[]{2, 3}; System.out.println(test(array)); } public static int test(int[] array) { int result = 0; int limit = (int) Math.pow(2, array.length); for (int i = 1; i < limit; i++) { int sum = 0; for (int j = 0; j < array.length; j++) { int num = i >> j; if ((num & 1) != 0) { sum += array[j]; } } if (checkPrim(sum)) { System.out.println(sum); result++; } } return result; } public static boolean checkPrim(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return true; } }