I am learning java myself these days. This function is to calculate the Combination. However, I found that there is a very small limit of the number n and k in this function. Each time if I type a large n or k, for instance, 100, it gives me
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Combination.combination(Combination.java:29)
at Combination.main(Combination.java:47)
Or gives me a negative number…
Is there a way to make it work for large number like 10000?
Thanks!
import java.util.HashMap; import java.util.Map;
public class Combination {
private Map<Long,Long> factorialMap = new HashMap<Long,Long>();
public Long getFactorial(int number) {
Long val = factorialMap.get(number);
if(val != null) {
return val;
} else {
val = getFactorialRecursive(number);
factorialMap.put((long) number, val);
return val;
}
}
public Long getFactorialRecursive(int number) {
if(number == 1 || number == 0) {
return 1L;
} else {
return number * getFactorialRecursive(number-1);
}
}
public Long combination(int fromVal, int chooseVal) {
return getFactorial(fromVal)/(getFactorial(chooseVal)*getFactorial(fromVal-chooseVal));
}
public static void main(String[] args) {
int n, k;
Combination comb = new Combination();
java.util.Scanner console = new java.util.Scanner(System.in);
while (true) // will break with k > n or illegal k or n
{ System.out.print ("Value for n: ");
n = console.nextInt();
if ( n < 0 ) break;
System.out.print ("Value for k: ");
k = console.nextInt();;
if ( k > n || k < 0 )
break;
System.out.print(n +" choose " + k + " = ");
System.out.println(comb.combination(n,k));
}
console.nextLine(); // Set up for "Press ENTER...
} }
You should use the
BigIntegerobject: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.htmlIn particular, your problem is that 21! is too large for even a long and therefore overflows. Another option would be to use a double, but that will lose precision, so if you need integer accuracy
BigIntegeris the way to go.Using
BigIntegeryou will need to convert your integer toBigInteger:Then use the
add,multiply,divideandsubtract(amongst others) to manipulate your values (like):Then you can use the method
longValue()to get the value back (assuming it fits in a long):