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;
}
}
在 java.util.HashMap.get(来源不明)异常线程 “main” java.lang.StackOverflowError的这是什么原因为stackoverflow异常?
嗨, 为什么我收到计算器例外程序?
我知道,stackoverflow通常意味着你有一个无限循环, ,但这工作正常,当我使用10000或其他数字较小,突然无限大数字?
你可以计算阶乘'(10000)''然后阶乘(20000)'。 ;)(这个代码是线程敌对的,顺便说一句,对于负数'n'有点奇怪。) – 2012-02-05 12:36:49