Skip to content
AI360Xpert
Beta

Factorials

A factorial multiplies every whole number down to 1, so it counts every possible ordering, and it grows so fast that small inputs give huge answers.

Five cross-validation folds can shuffle into 120 orders because the first slot has 5 choices, the next 4, then 3, 2, 1, and that shrinking product 5 times 4 times 3 times 2 times 1 is 5 factorial
Five cross-validation folds can shuffle into 120 orders because the first slot has 5 choices, the next 4, then 3, 2, 1, and that shrinking product 5 times 4 times 3 times 2 times 1 is 5 factorial

Why Does This Exist?

Every arrangement count in ML bottoms out at a factorial. The number of orders of nn items is n!n!, and the permutation and combination formulas are just fractions of factorials with cancellations. If n!n! feels like notation rather than a quantity, those formulas stay mysterious and their edge cases, like choosing all nn items, look like coincidences.

Factorials also teach a practical lesson about growth. They outrun exponentials, overflow fixed-width integers shockingly early, and explain why exhaustive search over orderings dies around n=12n = 12. The ordering counts behind permutations and combinations are this page's main customer.

The one idea you need first

An ordering of 3 items A, B, C is a sequence like B, A, C. The first slot has 3 candidates, the second has the 2 remaining, the last takes whatever is left: 3×2×1=63 \times 2 \times 1 = 6 orders. That shrinking product is the definition.

Think of It Like This

Dealing out finishing medals

Five runners finish a race and you award gold, silver, bronze, fourth, and fifth. Gold has 5 candidates. Silver has the 4 who did not win gold. Bronze has 3, then 2 for fourth, and the last runner takes fifth unopposed. Total medal assignments: 5×4×3×2×1=1205 \times 4 \times 3 \times 2 \times 1 = 120.

Notice the last slot contributes a factor of 1: no choice, but the multiplication keeps its shape. That trailing 1 is the same reason mathematicians set 0!=10! = 1. It keeps every formula working when the choices run out, the way multiplying by 1 keeps a product intact.

How It Actually Works

The factorial of a whole number nn multiplies every positive whole number down to 1:

n!=n×(n−1)×⋯×2×1,0!=1n! = n \times (n-1) \times \cdots \times 2 \times 1, \qquad 0! = 1

In plain words: n!n! counts the orderings of nn distinct items, and 0!=10! = 1 because there is exactly one way to arrange nothing. That convention also makes (nn)=n!/(n! 0!)=1\binom{n}{n} = n!/(n! \, 0!) = 1 come out right: one way to take everything.

Worked example

Shuffle 5 cross-validation folds into a processing order.

5!=5×4×3×2×1=1205! = 5 \times 4 \times 3 \times 2 \times 1 = 120 orders. Step by step: 5×4=205 \times 4 = 20, 20×3=6020 \times 3 = 60, 60×2=12060 \times 2 = 120, 120×1=120120 \times 1 = 120.

Grow it slightly: 10!=3,628,80010! = 3{,}628{,}800. Ten items already have over three and a half million orders, which is why brute-forcing the best ordering stops being a plan long before datasets get big. For scale, 20!=2,432,902,008,176,640,00020! = 2{,}432{,}902{,}008{,}176{,}640{,}000, about 2.4×10182.4 \times 10^{18}.

Watch Out For

Overflowing integers with innocent-looking factorials

The symptom is a negative or wrapped count from exact integer code: 21!21! exceeds the int64 maximum of about 9.2×10189.2 \times 10^{18}, since 20!20! already reaches 2.4×10182.4 \times 10^{18}. The fix is to never materialize the factorial: cancel first in n!/k!n!/k! expressions, work in log space with sums of logarithms, or use a library log-gamma function. If you only need to compare two huge counts, compare their logs.

Treating 0! as 0

Zero items feels like zero ways, but 0!=10! = 1: the empty arrangement exists exactly once. The symptom is a combination formula that divides by zero or returns 0 at the boundary, like claiming (55)=0\binom{5}{5} = 0 ways to take all five features. Trust the convention, because every choose formula depends on it, and test boundary inputs k=0k = 0 and k=nk = n explicitly.

The Quick Version

  • n!n! multiplies nn down to 1 and counts the orderings of nn distinct items.
  • 5!=1205! = 120, 10!=3,628,80010! = 3{,}628{,}800, and growth beats exponentials fast.
  • 0!=10! = 1 because the empty arrangement exists exactly once, keeping choose formulas valid at the edges.
  • Permutation and combination formulas are cancelled fractions of factorials.
  • Never compute huge factorials directly: cancel first or work in log space.
  • 21!21! overflows int64, so integer code needs care past n=20n = 20.