Meta.Numerics Library
Factorial Method (n)
Meta.NumericsMeta.Numerics.FunctionsAdvancedIntegerMathFactorial(Int32)
Computes the factorial of an integer.
Declaration Syntax
C#Visual BasicVisual C++F#
public static double Factorial(
	int n
)
Public Shared Function Factorial ( _
	n As Integer _
) As Double
public:
static double Factorial(
	int n
)
static member Factorial : 
        n:int -> float 
Parameters
n (Int32)
The argument, which must be non-negative.
Return Value
The factorial n!.
Remarks

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 intrepretation 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 overlow an int. 21! would overflow a long. 171! overflows even a double.)

In order to deal with factorials of larger runbers, 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)).

Exceptions
ExceptionCondition
ArgumentOutOfRangeExceptionn is negative.
See Also