factorial

n!

factorial(n)

Try it yourself:


Calculate products of integers with factorial

factorial(n), written n!, multiplies every positive integer up to n. Thus 5! equals 120, and 0! is 1. Factorials are fundamental in counting arrangements, probability, and series formulas.

Counting applications

The number of orderings of n distinct objects is n!. For selections, use combinations; for ordered selections, use permutations. These functions avoid manually forming large factorial ratios. gamma extends the factorial idea beyond non-negative integers.

Input limits

For the usual combinatorial meaning, n must be a non-negative integer. Factorials grow extremely quickly, so even moderate inputs produce very large exact integers or can overflow in numeric systems. Avoid converting to ordinary floating point unless an approximation is intended. For logarithmic probability work, lgamma is often more stable.

All functions