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

intfactorial(int n)
int wrapper for #factorial(float)
return (int) factorial((float) n);
longfactorial(int n)
Utility method for computing the factorial n!
if (n < 0 || n > 20) {
    throw new IllegalArgumentException(
            "The factorial can only " + "be calculated for numbers n with 0 <= n <= 20 (currently n = " + n
                    + "). Please try to consider less modifications.");
if (n == 0 || n == 1) {
    return 1;
long f = 1;
for (int i = 2; i <= n; i++) {
    f *= i;
return f;
longfactorial(int n)
n!
long r = 1;
while (n > 0)
    r *= n--;
return r;
doublefactorial(int n)
factorial
if (n < 0) {
    throw new IllegalArgumentException("factorial can only handle n >= 0");
if (n > m_lastFactorial_id) {
    double result = factorials[m_lastFactorial_id]; 
    for (int i = factorials.length; i <= n; i++) {
        result = result * i; 
    return result;
} else {
    return factorials[n]; 
longfactorial(int n)
factorial
return (n < 2) ? n : (n * factorial(n - 1));
longfactorial(int n)
factorial
if (n < 0)
    throw new IllegalArgumentException();
if (n < 2)
    return 1;
long result = 1;
for (int i = n; i >= 2; i--)
    result *= i;
return result;
...
intfactorial(int n)
Find the factorial of the input number
if (n < 0) {
    throw new IllegalArgumentException("Input should be nonnegative.");
} else if (n < 2) {
    return 1;
} else {
    int accum = 1;
    for (int i = n; i > 0; i--)
        accum *= i;
...
intfactorial(int n)
factorial
int f = 1;
for (int i = 1; i <= n; i++) {
    f *= i;
return f;
longfactorial(int n)
factorial
long n_factorial = 1;
if (n == 1)
    return n_factorial;
if (n < 0)
    throw new IllegalArgumentException("Cannot calculate factorial of negative numbers!");
if (n > 20)
    throw new IllegalArgumentException("I can calculate up to 20!.");
for (int i = 2; i <= n; i++) {
...
doublefactorial(int n)
basically a lookup-function for n!
if (n < 0) {
    throw new IllegalArgumentException("factorial can only handle n >= 0");
if (n > LAST_DOUBLE_FACTORIAL) {
    throw new ArithmeticException(
            "Can't calculate a factorial for n larger then " + factorials.length + " within doubles.");
} else {
    return factorials[n]; 
...