Counting, Permutations & Combinations

A derivation-first toolkit for finite counting: sum, product, quotient and bijection principles; sequences, injections, permutations, cycles, combinations, multisets, and stars-and-bars edge cases.

Counting is rarely about arithmetic first. It is about defining the set of outcomes so precisely that two descriptions cannot denote the same outcome by accident. Once the object is clear—function, injection, subset, bijection, multiset, or orbit—the formula follows from a small number of proof principles.

Count objects, not stories #

For a finite set XX, its cardinality ∣X∣|X| is the number of elements. The safest way to show two finite families have the same size is a bijection: pair every object in XX with exactly one object in YY and vice versa.

This is why the following translations matter:

Informal outcome Precise object
length-rr code over alphabet AA function [r]→A[r]\to A
ordered choice of rr distinct items from AA injection [r]→A[r]\to A
unordered choice of rr distinct items rr-element subset of AA
rearrangement of all items of AA bijection A→AA\to A
unordered choice allowing repetition multiset of cardinality rr over AA

Here [r]={1,…,r}[r]=\{1,\ldots,r\}, with [0]=∅[0]=\varnothing. This typed view also exposes a common error: when r<nr<n, an ordered selection of rr objects from an nn-element set is an injection, not a bijection onto the whole nn-element set.

Four principles do most of the work #

Disjoint sum rule #

If A∩B=∅A\cap B=\varnothing, then

∣A∪B∣=∣A∣+∣B∣.|A\cup B|=|A|+|B|.

The disjointness condition is load-bearing. If outcomes can appear in both cases, adding counts double-counts the intersection; that is the territory of inclusion–exclusion.

For a partition X=X1∪˙⋯∪˙XkX=X_1\mathbin{\dot\cup}\cdots\mathbin{\dot\cup}X_k,

∣X∣=∑i=1k∣Xi∣.|X|=\sum_{i=1}^k|X_i|.

Product rule #

If an outcome is uniquely a pair (a,b)∈A×B(a,b)\in A\times B, then

∣A×B∣=∣A∣ ∣B∣.|A\times B|=|A|\,|B|.

For a sequence of stages, multiply the number of legal choices at each stage provided that the stage counts are valid for every preceding partial choice. If later choices depend on earlier ones, the factors may change, but the product rule still applies along each branch.

Complement and subtraction #

If B⊆AB\subseteq A, then

∣A∖B∣=∣A∣−∣B∣.|A\setminus B|=|A|-|B|.

This is often easier than describing the allowed outcomes directly: count all candidates, then subtract the forbidden set. The subset condition prevents meaningless negative or overlapping subtraction.

Uniform-fiber quotient rule #

Suppose a surjection q:X→Yq:X\to Y maps exactly kk objects of XX to every y∈Yy\in Y. The fibers partition XX into ∣Y∣|Y| blocks of size kk, so

∣Y∣=∣X∣k.|Y|=\frac{|X|}{k}.

“Divide by the number of reorderings” is valid only when every abstract outcome has the same number of labelled representations. Nonuniform symmetry groups are the point where naïve division fails.

The two questions that select the formula #

The diagram is not a mnemonic detached from meaning. Its four leaves are four different sets: all functions, injections, multisets, and subsets. If a problem mixes labelled roles, indistinguishable items, or equivalence under rotation, first refine the object; do not force it into the nearest leaf.

Ordered choices: powers and falling factorials #

Let AA have nn elements.

Repetition allowed: nrn^r #

A length-rr sequence is a function [r]→A[r]\to A. Every one of the rr positions independently has nn choices, hence

nr.n^r.

This includes the combinatorial convention n0=1n^0=1: there is exactly one empty sequence, or equivalently one function ∅→A\varnothing\to A. In particular, combinatorics uses 00=10^0=1 for the number of empty functions from the empty domain. Other analytic contexts may leave 000^0 undefined; the counted object resolves the convention here.

No repetition: the falling factorial #

An ordered selection of rr distinct items is an injection [r]→A[r]\to A. The first position has nn choices, the second n−1n-1, and so on:

(n)r=n(n−1)⋯(n−r+1)=n!(n−r)!,0≤r≤n.(n)_r=n(n-1)\cdots(n-r+1)=\frac{n!}{(n-r)!}, \qquad 0\le r\le n.

If r>nr>n, the count is 00: an injection from a larger finite set into a smaller one cannot exist. The factorial quotient should therefore not be applied outside its stated range.

Permutations are bijections #

If ∣A∣=n|A|=n, a permutation is an ordered selection of all nn elements:

n!=n(n−1)⋯2⋅1.n!=n(n-1)\cdots2\cdot1.

The recursion n!=n(n−1)!n!=n(n-1)! says: choose the first image in nn ways, then permute the remaining n−1n-1 elements. The base case 0!=10!=1 counts the unique empty bijection ∅→∅\varnothing\to\varnothing. Mathlib's factorial documentation points to the formal result that factorial counts permutations, while the NIST DLMF combinatorics chapter records standard permutation and cycle notation.

Cycles expose the mechanism #

A cycle

(a1 a2 … ak)(a_1\ a_2\ \ldots\ a_k)

maps a1↦a2,…,ak−1↦ak,ak↦a1a_1\mapsto a_2,\ldots,a_{k-1}\mapsto a_k,a_k\mapsto a_1 and fixes all unlisted elements. Repeatedly following a,σ(a),σ2(a),…a,\sigma(a),\sigma^2(a),\ldots must eventually return to aa because AA is finite and σ\sigma is injective. That orbit is one cycle. Starting from an unvisited element and repeating partitions AA into disjoint cycles.

Therefore every finite permutation decomposes into disjoint cycles. The decomposition is unique up to:

  • rotating the notation inside a cycle, such as (1 3 2)=(3 2 1)(1\ 3\ 2)=(3\ 2\ 1);
  • reordering disjoint cycles;
  • including or omitting fixed-point 11-cycles.

Disjoint cycles commute because each fixes every element moved by the other. Overlapping cycles generally do not.

The number of kk-cycles that can be formed from an nn-element set, for 1≤k≤n1\le k\le n, is

(nk)(k−1)!=n!k(n−k)!.\binom nk(k-1)!=\frac{n!}{k(n-k)!}.

Choose the kk participants, then arrange them around a directed cycle. There are k!k! linear listings but each cycle has kk rotational listings, leaving (k−1)!(k-1)!.

Unordered distinct choices: binomial coefficients #

An rr-combination from an nn-element set is an rr-element subset. Start with the (n)r(n)_r ordered selections. Every subset appears in exactly r!r! orders, so the uniform-fiber rule gives

(nr)=(n)rr!=n!r!(n−r)!,0≤r≤n.\binom nr =\frac{(n)_r}{r!} =\frac{n!}{r!(n-r)!}, \qquad 0\le r\le n.

For r>nr>n, define (nr)=0\binom nr=0. Formal verification that the rr-element subsets of an nn-element finite set have cardinality (nr)\binom nr appears as Finset.card_powersetCard in Mathlib's finite-powerset API.

Three identities, three bijections #

Complementing a chosen subset swaps its size from rr to n−rn-r:

(nr)=(nn−r).\binom nr=\binom n{n-r}.

Fix a distinguished element aa. Every rr-subset either omits aa or contains it:

(nr)=(n−1r)+(n−1r−1).\binom nr=\binom{n-1}r+\binom{n-1}{r-1}.

This is Pascal's recurrence, with the two cases disjoint. Finally, partition the whole power set by subset size:

2n=∣P(A)∣=∑r=0n(nr).2^n=|\mathcal P(A)|=\sum_{r=0}^n\binom nr.

The binomial theorem has the same classification. In expanding (x+y)n(x+y)^n, choose the rr factors that contribute yy; the other n−rn-r contribute xx:

(x+y)n=∑r=0n(nr)xn−ryr.(x+y)^n=\sum_{r=0}^n\binom nr x^{n-r}y^r.

Repeated objects: multinomial coefficients #

Suppose a word of length nn contains m1m_1 copies of type 1, …, mkm_k copies of type kk, where m1+⋯+mk=nm_1+\cdots+m_k=n. If all copies were temporarily labelled, there would be n!n! arrangements. Erasing labels within type ii identifies exactly mi!m_i! labelled arrangements, independently across types. Thus

(nm1,…,mk)=n!m1!⋯mk!.\binom{n}{m_1,\ldots,m_k}=\frac{n!}{m_1!\cdots m_k!}.

Equivalently, choose positions successively:

(nm1)(n−m1m2)⋯(mkmk)=n!m1!⋯mk!.\binom n{m_1} \binom{n-m_1}{m_2}\cdots \binom{m_k}{m_k} =\frac{n!}{m_1!\cdots m_k!}.

Unordered choices with repetition: stars and bars #

Choosing a multiset of size rr from nn labelled types is equivalent to solving

x1+⋯+xn=r,xi≥0,x_1+\cdots+x_n=r,\qquad x_i\ge0,

where xix_i is the multiplicity of type ii. For n≥1n\ge1, encode a solution by rr stars separated into nn bins by n−1n-1 bars. Every encoding contains r+n−1r+n-1 symbols, and choosing the rr star positions gives

( ⁣ ⁣(nr) ⁣ ⁣)=(n+r−1r)=(n+r−1n−1).\left(\!\!\binom nr\!\!\right) =\binom{n+r-1}{r} =\binom{n+r-1}{n-1}.

The left notation denotes multichoose, not an ordinary binomial coefficient. Mathlib formalizes the same identity as Nat.multichoose_eq in its binomial-coefficient library.

If every xix_i must be positive, set yi=xi−1≥0y_i=x_i-1\ge0:

x1+⋯+xn=r,xi≥1⟺y1+⋯+yn=r−n.x_1+\cdots+x_n=r, x_i\ge1 \quad\Longleftrightarrow\quad y_1+\cdots+y_n=r-n.

Hence the count is (r−1n−1)\binom{r-1}{n-1} when r≥n≥1r\ge n\ge1, and 00 when r<nr<n.

For n=0n=0, the stars-and-bars formula has a negative top index and must not be used. There is one empty multiset when r=0r=0, and none when r>0r>0.

Circular arrangements and the symmetry trap #

For n≥1n\ge1 distinct objects around an oriented circle, if rotations count as the same but reflections remain different, there are

n!n=(n−1)!\frac{n!}{n}=(n-1)!

arrangements. Every circular arrangement has exactly nn linear representations, one for each choice of starting point.

Mixed constraints: decompose one outcome exactly once #

When a count is uncertain, derive it in two structurally different ways. A mismatch usually reveals either a hidden order, a repeated representation, or overlapping cases.

Computing coefficients without giant factorials #

The factorial formula explains (nr)\binom nr, but it is a poor numerical algorithm. Use symmetry k=min⁡(r,n−r)k=\min(r,n-r) and the exact recurrence

(n0)=1,(ni)=(ni−1)n−i+1i(1≤i≤k).\binom n0=1, \qquad \binom ni=\binom n{i-1}\frac{n-i+1}{i} \quad(1\le i\le k).

Each step is an integer even though it is written as a product followed by division. In fixed-width code, reduce factors by greatest common divisors or use a big-integer type; computing n!n! first can overflow long before the final coefficient does.

Pascal's recurrence is preferable when many coefficients for the same nn are needed, and dynamic programming over the relevant state definition generalizes naturally to constrained counts. Recurrences and generating functions develop that next layer.

Common counting failures #

Symptom Hidden mistake Repair
answer differs by a factor of r!r! ordered sequences confused with subsets state whether positions/roles are labelled
divide-by-symmetry gives a fraction orbit/fiber sizes are not uniform inspect stabilizers; use a group-action method
product rule overcounts one final object has several construction histories make the decomposition unique or quotient the histories
sum rule overcounts cases overlap make cases disjoint or use inclusion–exclusion
factorial has a negative argument formula used for r>nr>n count injections as zero outside 0≤r≤n0\le r\le n
stars and bars handles distinct items indistinguishable stars were assumed use functions [r]→[n][r]\to[n] for labelled items
circular count divides by 2n2n automatically reflection equivalence or symmetry was not specified define the acting symmetries and fixed arrangements
sample rows determine a count unstated constraints were inferred from data count from declared rules, not accidental sample uniqueness

A reliable workflow #

  1. Define the outcome set, including labels, order, replacement, and equivalence.
  2. Check boundary cases: n=0n=0, r=0r=0, r>nr>n, and impossible constraints.
  3. Choose a bijection, disjoint partition, Cartesian product, complement, or uniform quotient.
  4. Prove that every legal outcome is generated and generated exactly once.
  5. Only then simplify the arithmetic.
  6. Cross-check with a second decomposition or a small exhaustive enumeration.

The formulas are consequences of this workflow. Memorizing them without their object models is what makes small wording changes so dangerous.

Sources

  1. bookSpecial Sections of Mathematics 1: Discrete Mathematics, Section 1, Topic 1pdfnotes
  2. docsNIST Digital Library of Mathematical Functions — Combinatorial Analysis
  3. docsMathlib — Binomial Coefficients
  4. docsMathlib — Finite Powersets and Combinations
  5. docsMathlib — Factorials and Falling Factorials
  6. docsMathlib — Cardinality of Finite Products

Searches titles, aliases, headings, summaries, and full text.↑↓ move ↵ open esc close