List of utility methods to do Factorial
long | factorial(long n) This function computes the factorial of an long integer long fac = 1; if (n < 0) { throw new RuntimeException("Underflow error in factorial"); } else if (n > 20) { throw new RuntimeException("Overflow error in factorial"); } else if (n == 0) { return 1; } else { ... |
long | factorial(long num) Gets the factorial of a number if (num == 0) return 1; if (num == 2 || num == 1) { return num; } else { return num * factorial(num - 1); |
double | factorialAsDouble(int n) factorial As Double double result = 1; for (int i = 1; i <= n; i++) { result *= (double) i; return result; |
double | factorialAsDoubleIncludeDivisor(int n, double divisor) Computes n! double result = 1.0 / divisor; for (int i = 1; i <= n; i++) { result *= (double) i; return result; |
int | factorialCheckBounds(int n) Compute n! long result = 1; for (int i = 1; i <= n; i++) { result *= (long) i; if (result > Integer.MAX_VALUE) { throw new Exception("n! causes integer overflow"); return (int) result; ... |
double | factorialDouble(final int n) Returns n!. if (n < 0) { throw new IllegalArgumentException("must have n >= 0 for n!"); return Math.floor(Math.exp(factorialLog(n)) + 0.5); |
double | factorialLog(final int n) Returns the natural logarithm of n!. if (n < 0) { throw new IllegalArgumentException("must have n > 0 for n!"); double logSum = 0; for (int i = 2; i <= n; i++) { logSum += Math.log((double) i); return logSum; ... |
double | factorialLog(final int n) Returns the natural logarithm of n!. if (n < 0) { return 0; if (n < 21) { return Math.log(factorial(n)); double logSum = 0; for (int i = 2; i <= n; i++) { ... |