AdvancedIntegerMathFactorial Method |
Namespace: Meta.Numerics.Functions
Exception | Condition |
---|---|
ArgumentOutOfRangeException | n is negative. |
The factorial of an integer n is the product of all integers from 1 to n. For example, 4! = 4 * 3 * 2 * 1 = 24.
n! also has a combinatorial interpretation as the number of permutations of n objects. For example, a set of 3 objects (abc) has 3! = 6 permutations: (abc), (bac), (cba), (acb), (cab), (bca).
Because n! grows extremely quickly with increasing n, we return the result as a Double, even though the value is always an integer. (13! would overflow an int, 21! would overflow a long, 171! overflows even a double.)
In order to deal with factorials of larger numbers, you can use the LogFactorial(Int32) method, which returns accurate values of ln(n!) even for values of n for which n! would overflow a double.
The factorial is generalized to non-integer arguments by the Γ function (Gamma(Double)).