Factorial (source code)

= Factorial
{title2=$n!$}
{wiki}

For a <nonnegative integer> $n$, the <factorial> $n!$ is the <product> of the <positive integers> from $1$ to $n$, with the <empty product> equal to $1$:
$$
n!=\prod_{j=1}^{n}j,\qquad 0!=1.
$$
Thus $1!=1$, $2!=2$, $3!=6$, $4!=24$ and $5!=120$. The <factorial> counts the <permutations> of $n$ distinct objects: arranging $A,B,C$ gives exactly $ABC,ACB,BAC,BCA,CAB,CBA$. The choice tree in <permutation> shows the three choices for the first position, two for the second, and one for the last.

The value $0!=1$ has a counting explanation: the <empty set> has exactly one ordering, the empty list. Equivalently, it has exactly one <bijection> to itself, the empty <function>. There are no choices to make, and one completed outcome. The <factorial> therefore obeys the <recurrence relation>
$$
0!=1,\qquad n!=n(n-1)!\quad(n\geq1).
$$
The useful distinction is how many positions are filled. For <integers> $0\leq k\leq n$, choosing and ordering $k$ distinct objects by <sampling without replacement> gives the <falling factorial>, while choosing an unordered $k$-element <subset> gives the <binomial coefficient>:
$$
\underbrace{n(n-1)\cdots(n-k+1)}_{k\text{ factors}}=\frac{n!}{(n-k)!},\qquad
\binom nk=\frac{n!}{k!(n-k)!}.
$$
For five people, assigning captain and deputy gives $5!/3!=20$ outcomes; choosing a two-person team gives $\binom52=10$. Every team has exactly $2!$ assignments of the two roles, explaining the division. When $k=0$, both counts are one. The <factorial> is useful here because one count of full orderings yields both formulas by cancellation and by forgetting order. https://openstax.org/books/college-algebra-2e/pages/9-5-counting-principles[OpenStax, College Algebra 2e, section 9.5: counting ordered selections and combinations]{ref}

Distinctness matters. If the two copies of $A$ in $AAB$ are indistinguishable, the visible arrangements are just $AAB,ABA,BAA$, numbering $3!/2!=3$. Temporarily labelling them $A_1,A_2$ gives six <permutations>, but exchanging the two labels leaves each visible word unchanged, so every word was counted $2!$ times.

\b[Proof outline.] For $n\geq1$, choose the first object in $n$ ways, then order the remaining $n-1$ objects in $(n-1)!$ ways. The <rule of product> gives the <recurrence relation>; repeated application gives the displayed <product>, starting from the one empty ordering. This explains why the <factorial> grows quickly: every additional distinct object introduces another multiplicative factor.