true
if there exists a non-empty subset of
* t
whose elements sum to zero.
*/
public static boolean subsetSum(Seta + b
where null references are treated as
* being equal to zero.
*
* @param a
* @param b
* @return the sum of a
and b
*/
private static Integer add(Integer a, Integer b) {
if (a == null && b == null) {
return null;
} else if (a == null) {
return b;
} else if (b == null) {
return a;
}
return a + b;
}
/**
* Recursive solution for the subset sum problem.
*
*
* currentSum
holds the value of the subset under consideration;
* it should be null
if the current subset is empty.
*
* @param t
* a list containing unique integer values (a set of integers)
* @param currentSum
* the current subset sum so far
* @return true
if there exists a subset of t
such
* that the sum of the elements in the subset and
* currentSum
equals zero.
*/
private static boolean subsetSum(Listleft
and right
*/
public static LinkedListtrue
if n
is prime, false
* otherwise
*/
public static boolean isPrime(int x) {
return isPrime(x, 2);
}
/**
* Recursively determine if an integer is prime.
*
* @param n
* an integer value greater than 2
* @param divisor
* @return true
if n
is prime, false
* otherwise
*/
private static boolean isPrime(int x, int divisor) {
if (divisor > Math.sqrt(x)) {
return true;
}
if (x % divisor == 0) {
return false;
}
return isPrime(x, divisor + 1);
}
/**
* Find the minimum element in a list.
*
* @param t a list of integers
* @return the minimum element in the list
*/
public static int min(Lista % b
*/
public static int remainder(int a, int b) {
if (a < b) {
return a;
}
return remainder(a - b, b);
}
/**
* Integer division.
*
* @param a an integer greater than zero
* @param b an integer greater than zero
* @return the value a / b
*/
public static int divide(int a, int b) {
if (a < b) {
return 0;
}
return 1 + divide(a - b, b);
}
}