用java计算任意大的阶乘:
下面是源程序:
import java.math.BigInteger;
import java.util.*;
public class Compute {
protected static ArrayList table=new ArrayList();
static {
//用1初始化缓存的第一个元素
table.add(BigInteger.valueOf(1));
}
public static synchronized BigInteger factor(int x){
if(x<0) throw new IllegalArgumentException("x must be non-negative");
for(int size=table.size();size<=x;size++){
BigInteger lastfact=(BigInteger)table.get(size-1);
BigInteger nextfact=lastfact.multiply(BigInteger.valueOf(size));
table.add(nextfact);
System.out.println(x+"---------"+table.size());
}