Java Utililty Methods Factorial

List of utility methods to do Factorial

Description

The list of methods to do Factorial are organized into topic(s).

Method

longfactorial(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 {
...
longfactorial(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);
doublefactorialAsDouble(int n)
factorial As Double
double result = 1;
for (int i = 1; i <= n; i++) {
    result *= (double) i;
return result;
doublefactorialAsDoubleIncludeDivisor(int n, double divisor)
Computes n!
double result = 1.0 / divisor;
for (int i = 1; i <= n; i++) {
    result *= (double) i;
return result;
intfactorialCheckBounds(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;
...
doublefactorialDouble(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);
doublefactorialLog(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;
...
doublefactorialLog(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++) {
...