import java.math.BigInteger;
import java.util.HashMap;
/**
*
* @author cypronmaya
*/
public class test {
static HashMap<Integer, BigInteger> cache = new HashMap<Integer, BigInteger>();
public static void main(String[] args) {
System.out.println(factorial(20000));
}
public static BigInteger factorial(int n) {
BigInteger ret;
if (n == 0) {
return BigInteger.ONE;
}
if (null != (ret = cache.get(n))) {
return ret;
}
ret = BigInteger.valueOf(n).multiply(factorial(n - 1));
cache.put(n, ret);
return ret;
}
}
Exception in thread "main" java.lang.StackOverflowError at java.util.HashMap.get(Unknown Source)
Hi, Why am i getting stackoverflow exception to this program?
i know that stackoverflow usually means you have an infinite loop, but this works fine when i'm using 10000 or some other numbers lesser, wht becomes suddenly infinite with big numbers?