Class Factorial

java.lang.Object
org.apache.commons.numbers.combinatorics.Factorial

public final class Factorial extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final double[]
    All factorials that can be represented as a double (values up to 170!).
    (package private) static final long[]
    All long-representable factorials.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Private constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    doubleValue(int n)
    Computes the factorial of n.
    (package private) static double
    Return the factorial of n.
    static long
    value(int n)
    Computes the factorial of n.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FACTORIALS

      static final long[] FACTORIALS
      All long-representable factorials.
    • DOUBLE_FACTORIALS

      private static final double[] DOUBLE_FACTORIALS
      All factorials that can be represented as a double (values up to 170!).
  • Constructor Details

    • Factorial

      private Factorial()
      Private constructor.
  • Method Details

    • value

      public static long value(int n)
      Computes the factorial of n.
      Parameters:
      n - Argument.
      Returns:
      n!
      Throws:
      IllegalArgumentException - if n < 0.
      IllegalArgumentException - if n > 20 (the factorial value is too large to fit in a long).
    • doubleValue

      public static double doubleValue(int n)
      Computes the factorial of n.

      The result should be small enough to fit into a double: The largest n for which n! does not exceed Double.MAX_VALUE is 170. Double.POSITIVE_INFINITY is returned for n > 170.

      Parameters:
      n - Argument.
      Returns:
      n!
      Throws:
      IllegalArgumentException - if n < 0.
      Since:
      1.1
    • uncheckedFactorial

      static double uncheckedFactorial(int n)
      Return the factorial of n.

      Note: This is an internal method that exposes the tabulated factorials that can be represented as a double. No checks are performed on the argument.

      Parameters:
      n - Argument (must be in [0, 170])
      Returns:
      n!