SciPy

scipy.special.factorial

scipy.special.factorial(n, exact=False)[source]

The factorial function, n! = special.gamma(n+1).

If exact is 0, then floating point precision is used, otherwise exact long integer is computed.

  • Array argument accepted only for exact=False case.
  • If n<0, the return value is 0.
Parameters:

n : int or array_like of ints

Calculate n!. Arrays are only supported with exact set to False. If n < 0, the return value is 0.

exact : bool, optional

The result can be approximated rapidly using the gamma-formula above. If exact is set to True, calculate the answer exactly using integer arithmetic. Default is False.

Returns:

nf : float or int

Factorial of n, as an integer or a float depending on exact.

Examples

>>> from scipy.special import factorial
>>> arr = np.array([3, 4, 5])
>>> factorial(arr, exact=False)
array([   6.,   24.,  120.])
>>> factorial(5, exact=True)
120L

Previous topic

scipy.special.expi

Next topic

scipy.special.factorial2