← All Posts
Math · Probability & Statistics · Combinatorial Analysis· Part 3 of 7

Permutations

A permutation is an ordered arrangement of a set of objects. Whenever order matters — books on a shelf, runners crossing a finish line, characters in a string — you are counting permutations. This post derives every permutation formula from scratch using the basic principle of counting from Part 2.

Arrangements of $n$ Distinct Objects: $n!$

Suppose we have $n$ distinct objects and want to put them in a row. By the multiplication rule:

Number of permutations of $n$ distinct objects:

$n \cdot (n-1) \cdot (n-2) \cdots 2 \cdot 1 = n!$

The factorial $n!$ is read “n factorial”. By convention $0! = 1$, which is the right value because there is exactly one way to arrange the empty set: the empty arrangement.

Factorials grow alarmingly fast. $10! = 3{,}628{,}800$ and $20! \approx 2.4 \times 10^{18}$, which is already too large for a 64-bit integer when you reach $21!$. In probability calculations factorials usually cancel; in algorithms you precompute them modulo a prime.

▶ Visual: Filling Slots One by One

We arrange 4 distinct letters $\{A, B, C, D\}$ in 4 slots. Step through to watch each factor of $4 \cdot 3 \cdot 2 \cdot 1$ appear.

Choosing $r$ from $n$: $P(n,r)$

What if we only fill $r$ slots out of $n$ available items? The same logic applies, but we stop after $r$ stages:

$P(n,r) = n \cdot (n-1) \cdots (n - r + 1) = \dfrac{n!}{(n - r)!}.$

The denominator $(n-r)!$ "cancels" the factors we never used.

$P(n,r)$ — permutations of $n$ taken $r$ at a time:

$P(n,r) = \dfrac{n!}{(n-r)!}$, valid for $0 \le r \le n$.

Worked: Olympic podium

Eight runners compete in a 100m final. How many ways are the gold, silver, and bronze medals awarded?

Order matters: gold–silver–bronze is different from silver–gold–bronze. With $n = 8$ and $r = 3$:

$P(8, 3) = \dfrac{8!}{5!} = 8 \cdot 7 \cdot 6 = 336.$

Worked: 4-digit PIN with no repeated digits

How many 4-digit PINs use digits 0–9 with no digit repeated?

$P(10, 4) = 10 \cdot 9 \cdot 8 \cdot 7 = 5040.$ Note that this is order-sensitive: PIN 1234 differs from 4321.

Permutations of Indistinguishable Objects

What if some of the objects are identical? Counting all $n!$ orderings now overcounts, because swapping two identical objects produces the same arrangement.

Theorem. The number of distinct permutations of $n$ objects, where there are $n_1$ identical of type 1, $n_2$ identical of type 2, ..., $n_k$ identical of type $k$ (with $n_1 + n_2 + \dots + n_k = n$), is

$\dfrac{n!}{n_1!\, n_2! \cdots n_k!}.$

Why divide by $n_1! n_2! \cdots n_k!$?

Pretend, for a moment, that all $n$ objects are distinct (label the identical As as $A_1, A_2, \dots$; do the same for each repeated letter). There are $n!$ ordered arrangements of these labeled objects.

Now, group two arrangements together if they look the same once we erase the labels. How many labeled arrangements correspond to one unlabeled arrangement? We could permute the $n_1$ labels among the type-1 positions in $n_1!$ ways, the $n_2$ labels among the type-2 positions in $n_2!$ ways, and so on. So each unlabeled arrangement maps to $n_1! n_2! \cdots n_k!$ labeled ones. Dividing $n!$ by that count gives the unlabeled total.

Ross 1.3a — PEPPER

How many distinct ordered arrangements of the letters in the word PEPPER are there?

The word has $n = 6$ letters: P, P, P, E, E, R. So $n_1 = 3$ Ps, $n_2 = 2$ Es, $n_3 = 1$ R, summing to 6. The count is

$\dfrac{6!}{3!\,2!\,1!} = \dfrac{720}{6 \cdot 2 \cdot 1} = 60.$

Ross 1.3b — MISSISSIPPI

How many distinct arrangements are there of the letters in MISSISSIPPI?

$n = 11$ with $n_1 = 1$ M, $n_2 = 4$ Is, $n_3 = 4$ Ss, $n_4 = 2$ Ps. So

$\dfrac{11!}{1! \cdot 4! \cdot 4! \cdot 2!} = \dfrac{39{,}916{,}800}{1 \cdot 24 \cdot 24 \cdot 2} = 34{,}650.$

Circular Permutations

Suppose we seat $n$ people around a round table. Two seatings that are rotations of each other are considered the same — what matters is who sits next to whom, not the absolute orientation of the table.

If we naively used $n!$, we would overcount each arrangement $n$ times, once per rotation. So:

Number of circular permutations of $n$ people:

$\dfrac{n!}{n} = (n-1)!.$

An equivalent and often cleaner argument: fix one person at a designated seat. The remaining $n - 1$ people can be arranged in the other $n - 1$ seats in $(n-1)!$ ways.

Worked: round table

How many distinct seatings are there for 6 people around a round table?

$(6 - 1)! = 5! = 120.$

If reflections also count as the same (think of beads on a necklace that can be flipped), divide by another factor of 2 to obtain $\tfrac{(n-1)!}{2}$.

Why It Works (And When It Breaks)

Every permutation formula in this post is the same idea: count too much, then divide by the symmetry you do not care about.

SettingNaive countSymmetry to removeFinal
$n$ distinct in a row$n!$—$n!$
$r$ chosen, ordered$n!$order of unused $(n-r)!$$n!/(n-r)!$
Indistinguishable groups$n!$$n_i!$ within each group$n!/(n_1!\cdots n_k!)$
Circular$n!$$n$ rotations$(n-1)!$

This pattern — overcount, then divide — is the workhorse of all of combinatorics. We will reuse it in Part 4 to derive $\binom{n}{r}$ from $P(n,r)$.

A Note on Computing Permutations

For probability problems you usually leave permutations symbolic. For algorithms, you almost always need them modulo a large prime. The trick is to precompute factorials and inverse factorials once:

// O(N) precomputation, O(1) per query.
const int MAXN = 1'000'001;
const long long MOD = 1'000'000'007;
long long fact[MAXN], invFact[MAXN];

long long modpow(long long b, long long e, long long m) {
    long long r = 1;
    b %= m;
    while (e) {
        if (e & 1) r = r * b % m;
        b = b * b % m;
        e >>= 1;
    }
    return r;
}

void precompute() {
    fact[0] = 1;
    for (int i = 1; i < MAXN; i++) fact[i] = fact[i-1] * i % MOD;
    invFact[MAXN-1] = modpow(fact[MAXN-1], MOD - 2, MOD);
    for (int i = MAXN-2; i >= 0; i--) invFact[i] = invFact[i+1] * (i+1) % MOD;
}

long long P(int n, int r) {
    if (r < 0 || r > n) return 0;
    return fact[n] * invFact[n-r] % MOD;
}

Common Mistakes

Summary

Practice Problems