package common; import java.math.BigInteger; import java.util.*; public class Factorial4 { protected static ArrayList<BigInteger> table = new ArrayList <BigInteger> (); static { table.add(BigInteger.valueOf(1)); } public static void main (String args[]) { System.out.println(factorial(10)); } public static synchronized BigInteger factorial(int x) { 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); } return (BigInteger) table.get(x); } }