public static synchronized java.math.BigInteger | factorial(int x)The factorial() method, using BigIntegers cached in a Vector // create cache
table.addElement(BigInteger.valueOf(1));
if (x < 0) throw new IllegalArgumentException("x must be non-negative.");
for(int size = table.size(); size <= x; size++) {
BigInteger lastfact = (BigInteger)table.elementAt(size-1);
BigInteger nextfact = lastfact.multiply(BigInteger.valueOf(size));
table.addElement(nextfact);
}
return (BigInteger) table.elementAt(x);
|