日期:2014-05-20 浏览次数:20838 次
import java.math.BigInteger; import java.util.ArrayList; public class NewClass { protected static ArrayList alist = new ArrayList(); static { alist.add(BigInteger.valueOf(1)); } /** * Creates a new instance of factorial */ public static synchronized BigInteger factorial(int x) { if (x < 0) { throw new IllegalArgumentException("x must be non-negative."); } for (int size = alist.size(); size <= x; size++) { BigInteger lastfact = (BigInteger) alist.get(size - 1); BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size)); alist.add(nextfact); } return (BigInteger) alist.get(x); } public static void main(String[] args) { System.out.println("100 != " + factorial(100)); } }