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.
Why Does This Exist?
Every arrangement count in ML bottoms out at a factorial. The number of orders of items is , and the permutation and combination formulas are just fractions of factorials with cancellations. If feels like notation rather than a quantity, those formulas stay mysterious and their edge cases, like choosing all 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 . 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: 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: .
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 . 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 multiplies every positive whole number down to 1:
In plain words: counts the orderings of distinct items, and because there is exactly one way to arrange nothing. That convention also makes come out right: one way to take everything.
Worked example
Shuffle 5 cross-validation folds into a processing order.
orders. Step by step: , , , .
Grow it slightly: . 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, , about .
Watch Out For
Overflowing integers with innocent-looking factorials
The symptom is a negative or wrapped count from exact integer code: exceeds the int64 maximum of about , since already reaches . The fix is to never materialize the factorial: cancel first in 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 : the empty arrangement exists exactly once. The symptom is a combination formula that divides by zero or returns 0 at the boundary, like claiming ways to take all five features. Trust the convention, because every choose formula depends on it, and test boundary inputs and explicitly.
The Quick Version
- multiplies down to 1 and counts the orderings of distinct items.
- , , and growth beats exponentials fast.
- 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.
- overflows int64, so integer code needs care past .