public static double lnFactorial(int x){
  if (x < 0)   throw new IllegalArgumentException("Invalid input to factorial: " + x);
 else   if (x > 32)   return lnGamma(x + 1);
 else {
    int l=factorialsBuffer.size();
    for (int i=l; i <= x; i++) {
      factorialsBuffer.add(log(i) + factorialsBuffer.get(i - 1));
    }
    return factorialsBuffer.get(x);
  }
}

public static double lnFactorial ( int x ) { if ( x < 0 ) throw new IllegalArgumentException ( "Invalid input to factorial: " + x ) ; else if ( x > 32 ) return lnGamma ( x + 1 ) ; else { int l = factorialsBuffer . size ( ) ; for ( int i = l ; i <= x ; i ++ ) { factorialsBuffer . add ( log ( i ) + factorialsBuffer . get ( i - 1 ) ) ; } return factorialsBuffer . get ( x ) ; } }
get the log of the factorial of a number . upto 32 , the factorial is exact . after that , the gamma function is used because the numbers can ' t be represented anyway .
