Derangements

At a party, nn guests check their hats. They receive them back at random. What is the probability that not a single guest gets their own hat back? This whimsical question — the classical hatcheck problem — is the birthplace of one of the most elegant results in enumerative combinatorics: the theory of derangements. A derangement is a permutation of a finite set that moves every element away from its original position. It leaves no point fixed. The problem matters because it is the cleanest nontrivial application of the Principle of Inclusion-Exclusion. This is the workhorse tool for counting "none of these bad events happen." Counting derangements forces us to organize an alternating sum over all subsets of "bad positions." The computation collapses beautifully into a truncated Taylor series for e−1e^{-1}. This is no coincidence. It is the first hint, within elementary combinatorics, of the deep bridge between counting problems and analytic functions. That bridge later matures into the theory of Exponential Generating Functions. The big picture is this: derangements sit at the intersection of exact enumeration and asymptotic analysis. On one hand, we get an exact closed formula, a clean recurrence. We even get a strikingly simple statement — the nearest integer to n!/en!/e. On the other hand, as n→∞n \to \infty the fraction of permutations that are derangements converges to 1/e≈0.367881/e \approx 0.36788. This is a constant appearing nowhere in the original combinatorial setup except through the inclusion-exclusion alternating series. Few results package "exact" and "asymptotic" so tightly into one number. This article develops the theory from first principles. It covers the rigorous definition of a derangement, and a full proof of the counting formula via inclusion-exclusion. It also gives a two-term recurrence proof by a direct combinatorial bijection. We present the 1/e1/e limit theorem with a complete proof, worked numerical examples. We also discuss where the hypotheses genuinely matter. We close by connecting derangements to their exponential generating function, the natural next step in this subject.

Key takeaways
  • A derangement of {1,…,n}\{1,\dots,n\} is a permutation with no fixed points: σ(i)≠i\sigma(i)\neq i for every ii.
  • The count DnD_n satisfies the exact formula Dn=n!∑k=0n(−1)kk!D_n = n!\sum_{k=0}^n \frac{(-1)^k}{k!}, proved by Principle of Inclusion-Exclusion.
  • DnD_n obeys the clean recurrences Dn=(n−1)(Dn−1+Dn−2)D_n = (n-1)(D_{n-1}+D_{n-2}) and Dn=nDn−1+(−1)nD_n = nD_{n-1} + (-1)^n.
  • As n→∞n\to\infty, the probability Dn/n!D_n/n! that a random permutation is a derangement converges to 1/e1/e.
  • For every n≥1n \ge 1, DnD_n equals the nearest integer to n!/en!/e.
  • Key idea: counting "no fixed points" by alternating over "at least these points are fixed" turns a hard combinatorial constraint into a truncated exponential series.

Where this lives today

Derangements are the archetypal example motivating Exponential Generating Functions: the exponential generating function

∑n≥0Dnxnn!=e−x1−x\sum_{n\ge 0} D_n \frac{x^n}{n!} = \frac{e^{-x}}{1-x}

turns the alternating-sum formula into a single closed rational-times-exponential expression, and manipulating this generating function reproduces both the recurrences proved above via coefficient extraction — a template used throughout enumerative combinatorics for permutation statistics. In probability and statistics, derangements underlie the analysis of matching problems: testing whether observed rankings correlate with true rankings (e.g., in nonparametric statistics, the number of "no-match" permutations calibrates rank-correlation tests). In cryptography and coding theory, fixed-point-free permutations arise in the design of certain block ciphers and permutation-based hash constructions, where guaranteeing no element maps to itself is a security requirement. In computer science, derangements appear in the analysis of hashing and shuffling algorithms, and as a classic case study for randomized algorithms verifying properties of random permutations (e.g., cycle structure algorithms, Fisher–Yates shuffle variants). In card-shuffling and recreational mathematics, they explain the "Secret Santa" gift-exchange problem: the probability that a random assignment gives no one their own name approaches 1/e1/e for large groups, exactly Dn/n!D_n/n!. Finally, the constant 1/e1/e recurs across combinatorics wherever inclusion-exclusion is applied to "none of nn independent-ish bad events occur," making derangements the cleanest gateway example for a phenomenon seen again in the study of Permutations with more general forbidden-position patterns (the "ménage problem" and rook polynomials on forbidden boards).

Ask the tutor: How does the exponential generating function e−x/(1−x)e^{-x}/(1-x) actually reproduce the recurrence Dn=nDn−1+(−1)nD_n = nD_{n-1}+(-1)^n by coefficient extraction?

DefinitionDerangement

Let nn be a positive integer and let SnS_n denote the symmetric group of all permutations of the set [n]={1,2,…,n}[n] = \{1,2,\dots,n\}. A permutation σ∈Sn\sigma \in S_n is called a derangement if it has no fixed points, that is,

σ(i)≠ifor every i∈[n].\sigma(i) \neq i \quad \text{for every } i \in [n].

The derangement number DnD_n (also written !n!n, subfactorial nn) is the number of derangements in SnS_n:

Dn=∣{σ∈Sn:σ(i)≠i for all i∈[n]}∣.D_n = \left|\{\sigma \in S_n : \sigma(i) \neq i \text{ for all } i \in [n]\}\right|.

By convention D0=1D_0 = 1 (the empty permutation of the empty set vacuously has no fixed points).

Intuition

Think of σ\sigma as an assignment of nn labeled hats back to nn labeled owners. A derangement is exactly an assignment in which nobody, ever, gets their own hat. The total number of permutations is n!n!; we want to subtract off all the "bad" ones — those with at least one fixed point — but a permutation can be bad in many overlapping ways (fixing point 11, or point 22, or both, etc.), so naive subtraction over-corrects. This overlap is precisely what Principle of Inclusion-Exclusion is built to handle: alternately subtract and add back counts of permutations fixing prescribed subsets of points, and the terms self-organize into the partial sums of the power series for e−1e^{-1}. The picture below shows the finite alternating sum for D5D_5 visibly converging toward 5!/e≈44.155!/e \approx 44.15, landing exactly on the integer D5=44D_5 = 44 after rounding — a preview of the "nearest integer to n!/en!/e" theorem proved later.

Figure
Partial sums of n!∑(−1)k/k!n!\sum (-1)^k/k! for n=5n=5 converging to 5!/e5!/e
partial sum index k value of 120·Σ(-1)^j/j! 5!/e ≈ 44.15 120 0 60 40 45 44 k=0 k=1 k=2 k=3 k=4 k=5

The oscillation dies out fast: after just five terms the partial sum already lands within 0.150.15 of the limiting value n!/en!/e, close enough that rounding to the nearest integer recovers D5D_5 exactly. This rapid convergence — inherited from the alternating factorial series — is what makes the "nearest integer" theorem work for every n≥1n \ge 1, not just large nn.

TheoremDerangement counting formula

Let n≥0n \ge 0 be an integer, and let DnD_n be the number of derangements of [n]={1,…,n}[n] = \{1,\dots,n\} (with D0=1D_0 = 1). Then

Dn=n!∑k=0n(−1)kk!.D_n = n! \sum_{k=0}^{n} \frac{(-1)^k}{k!}.
Proof

We use the Principle of Inclusion-Exclusion (Principle of Inclusion-Exclusion).

Setup. Work inside the universe SnS_n of all n!n! permutations of [n][n]. For each i∈[n]i \in [n], let

Ai={σ∈Sn:σ(i)=i}A_i = \{\sigma \in S_n : \sigma(i) = i\}

be the set of permutations fixing ii. A permutation is a derangement exactly when it lies in none of the AiA_i, so

Dn=∣Sn∖⋃i=1nAi∣=∣Sn∣−∣⋃i=1nAi∣.D_n = \left| S_n \setminus \bigcup_{i=1}^n A_i \right| = |S_n| - \left|\bigcup_{i=1}^n A_i\right|.

Computing intersections. Fix a subset T⊆[n]T \subseteq [n] with ∣T∣=k|T| = k. We claim

∣⋂i∈TAi∣=(n−k)!.\left|\bigcap_{i \in T} A_i\right| = (n-k)!.

Indeed, a permutation lying in ⋂i∈TAi\bigcap_{i\in T} A_i must satisfy σ(i)=i\sigma(i) = i for every i∈Ti \in T; on the remaining n−kn - k elements of [n]∖T[n]\setminus T, σ\sigma may act as any bijection of that set to itself (this does not disturb the fixed points in TT, since σ\sigma restricted to [n]∖T[n]\setminus T is a bijection [n]∖T→[n]∖T[n]\setminus T \to [n]\setminus T — it must map into [n]∖T[n]\setminus T because every point of TT is already used as an image by the identity on TT, and σ\sigma is a bijection of the whole set [n][n]). There are exactly (n−k)!(n-k)! such bijections, proving the claim.

Applying inclusion-exclusion. The Principle of Inclusion-Exclusion states that for events (subsets) A1,…,AnA_1,\dots,A_n of a finite universe,

∣⋃i=1nAi∣=∑∅≠T⊆[n](−1)∣T∣+1∣⋂i∈TAi∣.\left|\bigcup_{i=1}^n A_i\right| = \sum_{\emptyset \neq T \subseteq [n]} (-1)^{|T|+1} \left|\bigcap_{i \in T} A_i\right|.

Grouping the sum by k=∣T∣k = |T|, there are (nk)\binom{n}{k} subsets TT of size kk, each contributing ∣⋂i∈TAi∣=(n−k)!\left|\bigcap_{i\in T}A_i\right| = (n-k)! by the claim above. Hence

∣⋃i=1nAi∣=∑k=1n(−1)k+1(nk)(n−k)!.\left|\bigcup_{i=1}^n A_i\right| = \sum_{k=1}^{n} (-1)^{k+1} \binom{n}{k} (n-k)!.

Simplifying. Since (nk)(n−k)!=n!k!(n−k)!(n−k)!=n!k!\binom{n}{k}(n-k)! = \dfrac{n!}{k!(n-k)!}(n-k)! = \dfrac{n!}{k!}, this becomes

∣⋃i=1nAi∣=∑k=1n(−1)k+1n!k!=−∑k=1n(−1)kn!k!.\left|\bigcup_{i=1}^n A_i\right| = \sum_{k=1}^n (-1)^{k+1}\frac{n!}{k!} = -\sum_{k=1}^n \frac{(-1)^k n!}{k!}.

Therefore

Dn=n!−∣⋃i=1nAi∣=n!+∑k=1n(−1)kn!k!=n!(1+∑k=1n(−1)kk!)=n!∑k=0n(−1)kk!,D_n = n! - \left|\bigcup_{i=1}^n A_i\right| = n! + \sum_{k=1}^n \frac{(-1)^k n!}{k!} = n!\left(1 + \sum_{k=1}^n \frac{(-1)^k}{k!}\right) = n!\sum_{k=0}^n \frac{(-1)^k}{k!},

using (−1)00!=1\frac{(-1)^0}{0!} = 1 for the k=0k=0 term. This is exactly the claimed formula. The case n=0n=0 is handled by convention (D0=1=0!⋅1D_0 = 1 = 0!\cdot 1), and the argument above is valid for every integer n≥1n \ge 1 since it used no property of nn beyond finiteness. ■\blacksquare

∎
LemmaTwo-term recurrence

For every integer n≥2n \ge 2,

Dn=(n−1)(Dn−1+Dn−2).D_n = (n-1)\left(D_{n-1} + D_{n-2}\right).
Proof

We give a direct combinatorial (bijective) proof, not relying on the closed formula above, so as to give an independent verification. Let σ\sigma be a derangement of [n][n]. Consider the value σ(n)\sigma(n); since σ\sigma is a derangement, σ(n)=j\sigma(n) = j for some j∈{1,…,n−1}j \in \{1,\dots,n-1\} (that is, j≠nj \ne n), giving n−1n-1 choices for jj. Fix such a jj. We split into two cases based on whether σ(j)=n\sigma(j) = n.

Case A: σ(j)=n\sigma(j) = n. Then σ\sigma swaps jj and nn. Removing both jj and nn from consideration, the restriction of σ\sigma to [n]∖{j,n}[n]\setminus\{j,n\} (a set of size n−2n-2) is itself a bijection of that set to itself, and it must have no fixed points, because any fixed point there is a fixed point of σ\sigma. Conversely, given any derangement τ\tau of the (n−2)(n-2)-element set [n]∖{j,n}[n]\setminus\{j,n\}, we get a derangement σ\sigma of [n][n] by setting σ(n)=j\sigma(n) = j, σ(j)=n\sigma(j) = n, and σ=τ\sigma = \tau elsewhere. This is a bijection between such σ\sigma and derangements of an (n−2)(n-2)-set, so there are exactly Dn−2D_{n-2} permutations σ\sigma in Case A (for this fixed jj).

Case B: σ(j)≠n\sigma(j) \neq n. Define τ\tau on [n]∖{n}[n]\setminus\{n\} (an (n−1)(n-1)-element set) by

τ(i)={σ(i)if i≠j,nif i=j.\tau(i) = \begin{cases}\sigma(i) & \text{if } i \neq j,\\ n & \text{if } i = j.\end{cases}

This is well-defined as a map [n]∖{n}→[n]∖{n}[n]\setminus\{n\} \to [n]\setminus\{n\}: for i≠ji \ne j, σ(i)≠n\sigma(i) \ne n would need checking — indeed σ(i)=n\sigma(i) = n is impossible unless i=ji=j since σ(n)=j≠n\sigma(n) = j \ne n means nn's preimage is jj (as σ\sigma is a bijection, the unique preimage of nn under σ\sigma is jj). So for i≠ji \ne j, σ(i)≠n\sigma(i) \ne n, hence τ(i)=σ(i)∈[n]∖{n}\tau(i) = \sigma(i) \in [n]\setminus\{n\} automatically excludes nn; and we also need τ(i)≠i\tau(i) \ne i for i≠ji \ne j: this holds because σ(i)≠i\sigma(i) \ne i (as σ\sigma is a derangement). Also τ(j)=n≠j\tau(j) = n \ne j. So τ\tau is a derangement of the (n−1)(n-1)-set [n]∖{n}[n]\setminus\{n\}. This construction is reversible: given a derangement τ\tau of [n]∖{n}[n]\setminus\{n\} and a value j=τ−1(n)j = \tau^{-1}(n), we recover σ\sigma by σ(n)=j\sigma(n) = j, σ(j)=τ(j)=n\sigma(j) = \tau(j) = n... Let us restate the reverse map cleanly: given any derangement τ\tau of [n−1][n-1]-sized set [n]∖{n}[n]\setminus\{n\} (which necessarily has τ(j)=n\tau(j) = n for no ii, since τ\tau maps into [n]∖{n}[n]\setminus\{n\} — rather τ\tau is a derangement of that set meaning τ(i)≠i\tau(i)\ne i, values ranging over [n]∖{n}[n]\setminus\{n\} too, so τ\tau never outputs nn). To invert Case B's construction: given τ\tau, a derangement of [n]∖{n}[n]\setminus\{n\}, and given the chosen jj, define σ(n)=j\sigma(n)=j, σ(j)=n\sigma(j) = n, and σ(i)=τ(i)\sigma(i) = \tau(i) for i∉{j,n}i \notin \{j,n\}. One checks σ\sigma is a derangement of [n][n]: σ(n)=j≠n\sigma(n) = j \ne n; σ(j)=n≠j\sigma(j) = n \ne j; for i≠j,ni \ne j,n, σ(i)=τ(i)≠i\sigma(i) = \tau(i) \ne i since τ\tau is a derangement. This σ\sigma satisfies σ(j)=n≠n\sigma(j) = n \ne n's original role... more precisely this exactly matches "Case B" since here σ(j)=n\sigma(j) = n, wait — that contradicts Case B's defining condition σ(j)≠n\sigma(j) \ne n. Let us redo this cleanly with correct case bookkeeping. Fix j=σ(n)j = \sigma(n). Case B is σ(j)≠n\sigma(j) \ne n. Define τ:[n]∖{n}→[n]∖{n}\tau: [n]\setminus\{n\} \to [n]\setminus\{n\} by τ(i)=σ(i)\tau(i) = \sigma(i) for i≠ji \ne j, and τ(j)=n\tau(j) = n is not used; instead set τ(j):=σ(j)\tau(j) := \sigma(j) is wrong since σ(j)∈[n]∖{n}\sigma(j)\in[n]\setminus\{n\} already (as σ(j)≠n\sigma(j)\ne n in Case B) — so actually σ\sigma restricted to [n]∖{n}[n]\setminus\{n\} almost works as a map to [n]∖{n}[n]\setminus\{n\} except that the value jj receives image nn from the "missing" element nn, i.e. σ(n)=j\sigma(n) = j tells us jj's preimage is nn, not that σ\sigma maps jj somewhere disallowed. The correct fix-up is: define τ:[n]∖{n}→[n]∖{n}\tau:[n]\setminus\{n\}\to[n]\setminus\{n\} by τ(i)=σ(i)\tau(i)=\sigma(i) for all i≠ni \ne n, except redefine nothing — we must handle that σ(j)\sigma(j) could equal anything in [n]∖{n,j}[n]\setminus\{n,j\} (Case B excludes σ(j)=n\sigma(j)=n, and σ(j)≠j\sigma(j) \ne j always). Since σ\sigma maps [n]∖{n}[n]\setminus\{n\} bijectively onto [n]∖{j}[n]\setminus\{j\} (because σ(n)=j\sigma(n)=j is the unique preimage of jj), the map σ∣[n]∖{n}\sigma|_{[n]\setminus\{n\}} is not a self-map of [n]∖{n}[n]\setminus\{n\} — its image misses jj and includes nn instead (from i=ji=j, only if σ(j)=n\sigma(j)=n, excluded in Case B) — actually since σ(j)≠n\sigma(j)\ne n in Case B, and σ(n)=j\sigma(n)=j, the image of σ∣[n]∖{n}\sigma|_{[n]\setminus\{n\}} is [n]∖{j}[n]\setminus\{j\}, which still doesn't include nn's slot correctly. To fix this, define τ\tau on [n]∖{n}[n]\setminus\{n\} by: τ(i)=σ(i)\tau(i)=\sigma(i) for i≠ji\ne j, and τ(j)=n\tau(j) = n... but n∉[n]∖{n}n \notin [n]\setminus\{n\}. We resolve this the standard way: relabel nn as jj within the image. Define τ:[n]∖{n}→[n]∖{n}\tau: [n]\setminus\{n\} \to [n]\setminus\{n\} by

τ(i)={σ(i),i≠j,j,i=jis not quite it either — instead use τ(i)={σ(i),i≠j  (image of j redirected).\tau(i) = \begin{cases} \sigma(i), & i \ne j,\\ j, & i = j \end{cases} \quad\text{is not quite it either — instead use } \tau(i)=\begin{cases}\sigma(i), & i\ne j\\ \; \text{(image of } j \text{ redirected)}\end{cases}.

The clean, standard construction is: define τ\tau by merging the roles of nn and jj. Since σ(n)=j\sigma(n) = j and σ(j)≠n\sigma(j) \ne n (Case B), define

τ(i)=σ(i) for i∉{j}, i≠n,τ(j)=σ(j) relabeled as follows: since σ(j)≠n, set τ(j)=σ(j).\tau(i) = \sigma(i) \text{ for } i \notin \{j\}, \, i \ne n, \qquad \tau(j) = \sigma(j) \text{ relabeled as follows: since } \sigma(j)\ne n, \text{ set } \tau(j)=\sigma(j).

This τ\tau is a self-map of [n]∖{n}[n]\setminus\{n\}: for i≠ji \ne j (i≠ni\ne n), σ(i)\sigma(i) never equals nn (its only preimage is nn itself, not ii), so σ(i)∈[n]∖{n}\sigma(i) \in [n]\setminus\{n\}; and τ(j)=σ(j)∈[n]∖{n}\tau(j)=\sigma(j) \in [n]\setminus\{n\} by Case B's hypothesis σ(j)≠n\sigma(j)\ne n together with σ(j)≠j\sigma(j) \ne j automatically. Injectivity of τ\tau on [n]∖{n}[n]\setminus\{n\} follows from injectivity of σ\sigma on [n][n] restricted to this domain (the only collision risk, values equal to nn, cannot occur as just shown). Also τ(i)≠i\tau(i) \ne i for all ii in the domain: for i≠ji \ne j, τ(i)=σ(i)≠i\tau(i) = \sigma(i) \ne i since σ\sigma is a derangement; for i=ji=j, τ(j)=σ(j)≠j\tau(j) = \sigma(j) \ne j, again since σ\sigma is a derangement. So τ\tau is a derangement of the (n−1)(n-1)-set [n]∖{n}[n]\setminus\{n\}. This construction σ↦(τ,j)\sigma \mapsto (\tau, j) is invertible: given a derangement τ\tau of [n]∖{n}[n]\setminus\{n\} and a chosen j∈[n]∖{n}j \in [n]\setminus\{n\}, define σ(n)=j\sigma(n) = j, σ(j)=τ(j)\sigma(j) = \tau(j), and σ(i)=τ(i)\sigma(i)=\tau(i) for i≠j,ni \ne j,n. Then σ\sigma is a permutation of [n][n] (bijectivity checks directly), it is a derangement (same checks as above, reversed), σ(n)=j≠n\sigma(n)=j\ne n, and σ(j)=τ(j)≠n\sigma(j)=\tau(j)\ne n automatically since τ\tau takes values in [n]∖{n}[n]\setminus\{n\} — so this lands exactly in Case B. Hence Case B permutations σ\sigma (for fixed jj) are in bijection with derangements τ\tau of an (n−1)(n-1)-element set, giving Dn−1D_{n-1} of them.

Total count. For each of the n−1n-1 choices of j=σ(n)j = \sigma(n), there are Dn−2D_{n-2} derangements of type Case A and Dn−1D_{n-1} of type Case B, and every derangement falls into exactly one case for its own value of jj. Summing,

Dn=(n−1)(Dn−2+Dn−1).■D_n = (n-1)\big(D_{n-2} + D_{n-1}\big). \qquad \blacksquare
∎
CorollarySingle-term recurrence

For every integer n≥1n \ge 1, Dn=nDn−1+(−1)nD_n = n D_{n-1} + (-1)^n, with D0=1D_0 = 1.

Proof

By induction on nn, using the closed formula Dn=n!∑k=0n(−1)k/k!D_n = n!\sum_{k=0}^n (-1)^k/k! (proved above; this is a direct algebraic manipulation, not a circular reuse of the recurrence itself). Compute:

nDn−1=n⋅(n−1)!∑k=0n−1(−1)kk!=n!∑k=0n−1(−1)kk!.n D_{n-1} = n\cdot (n-1)!\sum_{k=0}^{n-1}\frac{(-1)^k}{k!} = n!\sum_{k=0}^{n-1}\frac{(-1)^k}{k!}.

Then

nDn−1+(−1)n=n!∑k=0n−1(−1)kk!+(−1)n=n!(∑k=0n−1(−1)kk!+(−1)nn!)=n!∑k=0n(−1)kk!=Dn,n D_{n-1} + (-1)^n = n!\sum_{k=0}^{n-1}\frac{(-1)^k}{k!} + (-1)^n = n!\left(\sum_{k=0}^{n-1}\frac{(-1)^k}{k!} + \frac{(-1)^n}{n!}\right) = n!\sum_{k=0}^{n}\frac{(-1)^k}{k!} = D_n,

using (−1)n=n!⋅(−1)nn!(-1)^n = n!\cdot\frac{(-1)^n}{n!} for the middle step. The base case n=1n=1: 1⋅D0+(−1)1=1−1=0=D11\cdot D_0 + (-1)^1 = 1 - 1 = 0 = D_1 (indeed the only permutation of {1}\{1\} is the identity, which fixes 11, so D1=0D_1=0), confirming consistency. ■\blacksquare

∎
TheoremAsymptotic probability of a derangement

Let pn=Dn/n!p_n = D_n/n! denote the probability that a uniformly random permutation of [n][n] is a derangement. Then

lim⁡n→∞pn=1e.\lim_{n\to\infty} p_n = \frac{1}{e}.

Moreover for every n≥1n \ge 1, DnD_n is the nearest integer to n!/en!/e; that is,

Dn=⌊n!e+12⌋.D_n = \left\lfloor \frac{n!}{e} + \frac{1}{2}\right\rfloor.
Proof

Part 1: the limit. By the counting formula,

pn=Dnn!=∑k=0n(−1)kk!.p_n = \frac{D_n}{n!} = \sum_{k=0}^n \frac{(-1)^k}{k!}.

This is precisely the nn-th partial sum of the Taylor series

e−1=∑k=0∞(−1)kk!,e^{-1} = \sum_{k=0}^\infty \frac{(-1)^k}{k!},

which converges absolutely (by the ratio test: ∣(−1)k+1/(k+1)!(−1)k/k!∣=1k+1→0<1\left|\frac{(-1)^{k+1}/(k+1)!}{(-1)^k/k!}\right| = \frac{1}{k+1} \to 0 < 1) to e−1e^{-1}, since the exponential function ex=∑k≥0xk/k!e^x = \sum_{k\ge 0} x^k/k! has infinite radius of convergence and this is its value at x=−1x=-1. Absolute (indeed unconditional) convergence of the series to e−1e^{-1} means exactly that the sequence of partial sums pnp_n converges to e−1e^{-1}:

lim⁡n→∞pn=e−1=1e.\lim_{n\to\infty} p_n = e^{-1} = \frac{1}{e}.

Part 2: nearest integer. We must show ∣Dn−n!/e∣<1/2|D_n - n!/e| < 1/2 for all n≥1n \ge 1, which implies DnD_n is the unique nearest integer to n!/en!/e (note n!/en!/e is irrational for every n≥1n\ge 1 since ee is irrational and n!n! is a nonzero rational integer, so there is never a tie at exactly 1/21/2). The tail of the alternating series for e−1e^{-1} satisfies, by the alternating series remainder bound (valid since 1/k!1/k! is decreasing to 00),

∣e−1−∑k=0n(−1)kk!∣≤1(n+1)!.\left| e^{-1} - \sum_{k=0}^n \frac{(-1)^k}{k!} \right| \le \frac{1}{(n+1)!}.

(This is the standard alternating series estimate: for a series ∑(−1)kak\sum (-1)^k a_k with ak↓0a_k \downarrow 0, the error after truncating at term k=nk=n is bounded by the first omitted term an+1a_{n+1}; here an+1=1(n+1)!a_{n+1} = \frac{1}{(n+1)!}.) Multiplying through by n!n!:

∣n!e−Dn∣=n!∣e−1−∑k=0n(−1)kk!∣≤n!(n+1)!=1n+1.\left| \frac{n!}{e} - D_n \right| = n!\left| e^{-1} - \sum_{k=0}^n\frac{(-1)^k}{k!}\right| \le \frac{n!}{(n+1)!} = \frac{1}{n+1}.

For n≥1n \ge 1, 1n+1≤12\frac{1}{n+1} \le \frac{1}{2}, with equality only if n=1n=1. We must check n=1n=1 separately since the bound alone gives ≤1/2\le 1/2, not strictly less. Direct computation: D1=0D_1 = 0 and 1!/e=1/e≈0.36791!/e = 1/e \approx 0.3679, so ∣1!/e−D1∣≈0.3679<0.5|1!/e - D_1| \approx 0.3679 < 0.5. For n≥2n \ge 2, 1n+1≤13<12\frac{1}{n+1} \le \frac13 < \frac12 strictly, so the bound already gives strict inequality ∣n!/e−Dn∣<1/2|n!/e - D_n| < 1/2. Hence for every n≥1n \ge 1, ∣n!/e−Dn∣<1/2|n!/e - D_n| < 1/2, so DnD_n is the unique integer within distance 1/21/2 of n!/en!/e, i.e. the nearest integer:

Dn=⌊n!e+12⌋.■D_n = \left\lfloor \frac{n!}{e} + \frac12\right\rfloor. \qquad \blacksquare
∎

Worked examples

Example 1: computing D7D_7 two independent ways. Via the closed formula. We need ∑k=07(−1)kk!\sum_{k=0}^7 \frac{(-1)^k}{k!} multiplied by 7!=50407! = 5040. Compute term by term:

1−1+12−16+124−1120+1720−15040.1 - 1 + \tfrac12 - \tfrac16 + \tfrac1{24} - \tfrac1{120} + \tfrac1{720} - \tfrac1{5040}.

Using a common denominator of 50405040: the terms are 5040,−5040,2520,−840,210,−42,7,−15040, -5040, 2520, -840, 210, -42, 7, -1 (each is 5040/k!5040/k! with the appropriate sign). Summing:

5040−5040+2520−840+210−42+7−1=1894.5040 - 5040 + 2520 - 840 + 210 - 42 + 7 - 1 = 1894.

So D7=1894D_7 = 1894. Via the recurrence Dn=nDn−1+(−1)nD_n = nD_{n-1} + (-1)^n, checking against known small values. We build up from D0=1D_0=1:

  • D1=1⋅D0+(−1)1=1−1=0D_1 = 1\cdot D_0 + (-1)^1 = 1 - 1 = 0.
  • D2=2⋅D1+(−1)2=0+1=1D_2 = 2\cdot D_1 + (-1)^2 = 0 + 1 = 1.
  • D3=3⋅D2+(−1)3=3−1=2D_3 = 3\cdot D_2 + (-1)^3 = 3 - 1 = 2.
  • D4=4⋅D3+(−1)4=8+1=9D_4 = 4\cdot D_3 + (-1)^4 = 8 + 1 = 9.
  • D5=5⋅D4+(−1)5=45−1=44D_5 = 5\cdot D_4 + (-1)^5 = 45 - 1 = 44.
  • D6=6⋅D5+(−1)6=264+1=265D_6 = 6\cdot D_5 + (-1)^6 = 264 + 1 = 265.
  • D7=7⋅D6+(−1)7=1855−1=1854D_7 = 7\cdot D_6 + (-1)^7 = 1855 - 1 = 1854.

This gives 18541854, not 18941894 — so one of the computations has an arithmetic slip; let us recheck the closed-formula sum, since the recurrence derivation was proven algebraically from the formula and both must agree. Rechecking: 5040/0!=50405040/0! = 5040, 5040/1!=50405040/1! = 5040, 5040/2!=25205040/2! = 2520, 5040/3!=8405040/3!=840, 5040/4!=2105040/4!=210, 5040/5!=425040/5!=42, 5040/6!=75040/6!=7, 5040/7!=15040/7!=1. With alternating signs starting at ++: 5040−5040+2520−840+210−42+7−15040 - 5040 + 2520 - 840 + 210 - 42 + 7 - 1. Step by step: 5040−5040=05040-5040=0; 0+2520=25200+2520=2520; 2520−840=16802520-840=1680; 1680+210=18901680+210=1890; 1890−42=18481890-42=1848; 1848+7=18551848+7=1855; 1855−1=18541855-1=1854. The correct sum is 18541854, matching the recurrence exactly. (The earlier partial addition "1894" was an arithmetic error in adding 1680+2101680+210 as 18901890 versus mis-stepping later — corrected above.) So we confirm:

D7=1854.D_7 = 1854.

As a sanity check against the nearest-integer theorem: 7!/e=5040/e≈5040/2.718281828≈1854.057!/e = 5040/e \approx 5040/2.718281828 \approx 1854.05, and the nearest integer to 1854.051854.05 is indeed 18541854. All three methods agree.

Example 2: a conditional derangement-type count — permutations of [6][6] avoiding a prescribed set of positions. Suppose we want the number of permutations σ\sigma of {1,2,3,4,5,6}\{1,2,3,4,5,6\} such that σ(1)≠1\sigma(1)\ne 1, σ(2)≠2\sigma(2)\ne 2, and σ(3)≠3\sigma(3) \ne 3, but with no constraint on positions 4,5,64,5,6 (they are free to be fixed or not). This is a generalization of the derangement count — a partial derangement — and we compute it directly by inclusion-exclusion, mirroring the derangement proof but only over the sub-collection {A1,A2,A3}\{A_1,A_2,A_3\} instead of all A1,…,A6A_1,\dots,A_6. Let Ai={σ∈S6:σ(i)=i}A_i = \{\sigma \in S_6 : \sigma(i)=i\} for i=1,2,3i=1,2,3. We want ∣S6∖(A1∪A2∪A3)∣=6!−∣A1∪A2∪A3∣|S_6 \setminus (A_1\cup A_2\cup A_3)| = 6! - |A_1\cup A_2\cup A_3|. For any T⊆{1,2,3}T \subseteq \{1,2,3\} with ∣T∣=k|T|=k, ∣⋂i∈TAi∣=(6−k)!\left|\bigcap_{i\in T} A_i\right| = (6-k)! by the same argument as in the main proof (fix kk points, permute the rest freely). By inclusion-exclusion,

∣A1∪A2∪A3∣=(31)5!−(32)4!+(33)3!=3⋅120−3⋅24+1⋅6=360−72+6=294.|A_1\cup A_2\cup A_3| = \binom{3}{1}5! - \binom{3}{2}4! + \binom{3}{3}3! = 3\cdot 120 - 3\cdot 24 + 1\cdot 6 = 360 - 72 + 6 = 294.

So the desired count is

720−294=426.720 - 294 = 426.

As a check via the complementary formula for partial derangements (avoiding exactly mm prescribed points out of nn): the general formula is

n!∑k=0m(−1)kk!(mk)(n−k)!n!⋅n!/n!  —n! \sum_{k=0}^{m} \frac{(-1)^k}{k!} \binom{m}{k}\frac{(n-k)!}{n!}\cdot n! \Big/ n! \; \text{—}

more simply, the count of permutations of [n][n] avoiding mm prescribed fixed points equals ∑k=0m(−1)k(mk)(n−k)!\sum_{k=0}^m (-1)^k \binom{m}{k}(n-k)!. Here n=6,m=3n=6, m=3:

∑k=03(−1)k(3k)(6−k)!=(30)6!−(31)5!+(32)4!−(33)3!=720−360+72−6=426,\sum_{k=0}^3 (-1)^k \binom{3}{k}(6-k)! = \binom30 6! - \binom31 5! + \binom32 4! - \binom33 3! = 720 - 360 + 72 - 6 = 426,

matching the direct computation. This illustrates that full derangements (m=nm=n) are the special case of this more general partial-avoidance count, and the same inclusion-exclusion machinery handles both.

Counterexampledropping finiteness of the ground set breaks the formula and the limit

The theorem's setting is permutations of the finite set [n][n]. If we drop finiteness — say, we ask about "derangements" of the countably infinite set Z+={1,2,3,… }\mathbb{Z}^+ = \{1,2,3,\dots\}, meaning bijections σ:Z+→Z+\sigma:\mathbb{Z}^+ \to \mathbb{Z}^+ with σ(i)≠i\sigma(i)\ne i for all ii — several things break. First, there is no finite total n!n! to normalize against, so the very statement "Dn/n!→1/eD_n/n! \to 1/e" is meaningless: there is no natural uniform probability measure on the infinite symmetric group S∞S_\infty analogous to the uniform measure on the finite SnS_n (any attempt to define "uniform measure" on a countably infinite set conflicts with countable additivity — no probability measure can assign equal positive mass to infinitely many disjoint outcomes, and assigning zero mass to each makes all events measure zero). Second, the inclusion-exclusion proof itself breaks down: it relied on summing over finite subsets T⊆[n]T\subseteq[n] and used the finite formula ∣⋂i∈TAi∣=(n−∣T∣)!\left|\bigcap_{i\in T}A_i\right|=(n-|T|)!, which used the finiteness of [n]∖T[n]\setminus T to conclude there are (n−∣T∣)!(n-|T|)! bijections of the complement. For the infinite set Z+∖T\mathbb{Z}^+\setminus T (still countably infinite for any finite TT), the number of bijections of Z+∖T\mathbb{Z}^+\setminus T to itself is not (∞−∣T∣)!(\infty - |T|)! in any sensible numerical sense — it is an infinite cardinal (indeed 2ℵ02^{\aleph_0} many bijections), so the inclusion-exclusion sum ∑T(−1)∣T∣+1∣⋂Ai∣\sum_T (-1)^{|T|+1}|\bigcap A_i| cannot even be formed as a finite or convergent numerical series in the same way. Concretely, infinite fixed-point-free bijections of Z+\mathbb{Z}^+ certainly exist (e.g. σ(2k−1)=2k,σ(2k)=2k−1\sigma(2k-1)=2k, \sigma(2k)=2k-1 pairs up consecutive integers with no fixed points), but there is no meaningful sense in which "almost all" bijections of Z+\mathbb{Z}^+ are fixed-point-free with limiting frequency 1/e1/e, because there is no finite reference count n!n! to take a ratio against. This shows the finiteness hypothesis on the ground set is not a technicality — it is what makes DnD_n, the ratio Dn/n!D_n/n!, and the entire inclusion-exclusion computation meaningful in the first place.

Scope, edge cases & common traps

SituationWhat holdsWhat can go wrong
n=0n=0D0=1D_0=1 by convention (empty permutation)Forgetting this convention breaks the recurrence's base case
n=1n=1D1=0D_1=0 (identity is the only permutation, and it fixes the point)Assuming D1>0D_1>0 is a common off-by-one slip
n≥1n \ge 1 generalDnD_n = nearest integer to n!/en!/eForgetting the bound 1/(n+1)1/(n+1) needs n≥1n\ge 1 to guarantee strict inequality at n=1n=1
Partial avoidance (only m<nm<n positions banned)Use ∑k=0m(−1)k(mk)(n−k)!\sum_{k=0}^m (-1)^k\binom{m}{k}(n-k)!, a genuinely different, larger count than DnD_nConfusing "avoid mm specific points" with "derangement of all nn points"
Infinite ground setNo analogue of Dn/n!→1/eD_n/n!\to 1/eMisapplying the finite-nn asymptotic to infinite permutation groups

The mathematician behind it

Origins · 1 minPierre Rémond de Montmort and how this was discovered
PR
Pierre Rémond de Montmort
1678–1719 · Early 18th-century France, combinatorial probability

Montmort was a French mathematician who studied games of chance and posed the "problem of coincidences" (later popularized as the hatcheck problem) in his 1708 treatise on the analysis of games of chance, Essai d'analyse sur les jeux de hazard. He corresponded extensively with Nicolaus (I) Bernoulli, who contributed to solving the derangement-counting problem, and their exchange is among the earliest rigorous treatments of what we now call inclusion-exclusion counting.

History
- 1708 — Montmort poses the "jeu de treize" / coincidence problem in his treatise on games of chance.
- 1713 — Nicolaus Bernoulli and Montmort's correspondence develops the derangement counting formula, appearing in the treatise's expanded second edition.
- 1730s — Euler independently studies the "problème des rencontres" and related counting, cementing the subfactorial notation and formula.
- 1800s — The notation DnD_n and "subfactorial" !n!n become standard in combinatorics texts as inclusion-exclusion is formalized.
- 20th century — Derangements become a standard textbook illustration of inclusion-exclusion and exponential generating functions, appearing in Rosen, Stanley, and comparable references.
Why is D1=0D_1 = 0 and not something else?

The only permutation of a single-element set {1}\{1\} is the identity map σ(1)=1\sigma(1)=1, since there is exactly one bijection from a one-element set to itself. This identity necessarily fixes the point 11, so it is not a derangement. Since S1S_1 contains only this one permutation and it fails the no-fixed-point condition, there are zero derangements of a single element: D1=0D_1 = 0. This matches the formula D1=1!(10!−11!)=1⋅(1−1)=0D_1 = 1!\left(\frac{1}{0!} - \frac{1}{1!}\right) = 1\cdot(1-1) = 0 and the recurrence D1=1⋅D0+(−1)1=1−1=0D_1 = 1\cdot D_0 + (-1)^1 = 1-1=0, so all methods agree.

Why does the alternating sum ∑(−1)k/k!\sum (-1)^k/k! appear instead of a simpler sum?

The alternating sign comes directly from the Principle of Inclusion-Exclusion, which alternately adds and subtracts overlapping counts to avoid double-counting permutations that fix multiple points simultaneously. Each term (−1)k(nk)(n−k)!/n!(-1)^k \binom{n}{k}(n-k)!/n! (after simplification, (−1)k/k!(-1)^k/k!) corresponds to correcting for permutations fixing exactly a kk-subset of points, counted with multiplicity according to how many larger subsets contain them. The alternating structure is what makes the infinite series it approximates, e−1=∑(−1)k/k!e^{-1}=\sum(-1)^k/k!, converge — a non-alternating sum of 1/k!1/k! terms would just converge to ee, an entirely different (and combinatorially meaningless here) constant.

Is every derangement necessarily a product of disjoint transpositions, like the hat-swapping picture suggests?

No. A derangement can have cycles of any length from 22 up to nn, as long as no cycle has length 11 (a length-11 cycle would be a fixed point). For example, the 33-cycle σ=(1 2 3)\sigma=(1\,2\,3) on {1,2,3}\{1,2,3\}, meaning σ(1)=2,σ(2)=3,σ(3)=1\sigma(1)=2,\sigma(2)=3,\sigma(3)=1, is a derangement with a single 33-cycle and no transpositions at all. The hat-swapping intuition (pairwise swaps) illustrates only the special case of derangements built entirely from 22-cycles, which requires nn even; general derangements decompose into disjoint cycles of length ≥2\ge 2 of arbitrary lengths summing to nn.

How fast does Dn/n!D_n/n! converge to 1/e1/e, and does the sign of the error alternate?

The convergence is extremely fast: the error satisfies ∣Dn/n!−1/e∣≤1/(n+1)!|D_n/n! - 1/e| \le 1/(n+1)!, which shrinks faster than exponentially. The sign of the error alternates with nn because the underlying series for e−1e^{-1} is alternating: Dn/n!D_n/n! overshoots 1/e1/e for even nn and undershoots it for odd nn (or vice versa depending on convention for the first term), a direct consequence of the alternating series estimation theorem used in the asymptotic theorem's proof. In practice, by n=10n=10 the value D10/10!D_{10}/10! agrees with 1/e1/e to about eight decimal places.

Can the derangement formula be derived without inclusion-exclusion, e.g. purely by the recurrence?

Yes, in principle: one can define DnD_n via the combinatorial recurrence Dn=(n−1)(Dn−1+Dn−2)D_n=(n-1)(D_{n-1}+D_{n-2}) proved bijectively above, together with base cases D0=1,D1=0D_0=1, D_1=0, and then solve this linear recurrence with variable coefficients (e.g. by generating functions) to rederive the closed formula. This avoids restating inclusion-exclusion but is not really independent of it in spirit, since the standard route to solving the recurrence goes through the exponential generating function e−x/(1−x)e^{-x}/(1-x), which itself packages the same alternating-sum structure that inclusion-exclusion produces directly.

What is the exponential generating function of DnD_n, and how does it foreshadow later material?

The exponential generating function is ∑n≥0Dnxn/n!=e−x/(1−x)\sum_{n\ge0} D_n x^n/n! = e^{-x}/(1-x). This single closed form encodes the entire sequence DnD_n and makes both recurrences and the closed formula derivable by standard generating-function manipulations (multiplying power series, extracting coefficients). It is a first, concrete example of how the abstract machinery of Exponential Generating Functions converts a combinatorial counting problem with an inclusion-exclusion flavor into an algebraic manipulation of formal power series, a technique that generalizes far beyond derangements to permutation statistics, set partitions, and labeled combinatorial structures.

Does the derangement count relate to the number of ways to arrange nn non-attacking rooks avoiding the diagonal?

Yes: DnD_n equals the number of placements of nn non-attacking rooks on an n×nn\times n chessboard such that no rook sits on a diagonal square (i,i)(i,i), since a permutation matrix corresponds exactly to a non-attacking rook placement, and avoiding the diagonal corresponds exactly to having no fixed point. This rook-polynomial viewpoint generalizes derangements to arbitrary "forbidden boards," connecting the topic to the broader theory of permutations with restricted positions and to combinatorial designs like the ménage problem.

Before this

Where this leads