Counting derangements of a set amounts to the
hat-check problem, in which one considers the number of ways in which
n hats (call them
h1 through
hn) can be returned to
n people (
P1 through
Pn) such that no hat makes it back to its owner. Each person may receive any of the
n − 1 hats that is not their own. Call the hat which the person
P1 receives
hi and consider
his owner:
Pi receives either
P1's hat,
h1, or some other. Accordingly, the problem splits into two possible cases: •
Pi receives a hat other than
h1. This case is equivalent to solving the problem with
n − 1 people and
n − 1 hats because for each of the
n − 1 people besides
P1 there is exactly one hat from among the remaining
n − 1 hats that they may not receive (for any
Pj besides
Pi, the unreceivable hat is
hj, while for
Pi it is
h1). Another way to see this is to rename
h1 to
hi, where the derangement is more explicit: for any
j from 2 to
n,
Pj cannot receive
hj. •
Pi receives
h1. In this case the problem reduces to
n − 2 people and
n − 2 hats, because
P1 received
his hat and
Pi received
h1's hat, effectively putting both out of further consideration. For each of the
n − 1 hats that
P1 may receive, the number of ways that
P2, ...,
Pn may all receive hats is the sum of the counts for the two cases. This gives us the solution to the hat-check problem: Stated algebraically, the number D_n of derangements of an
n-element set is D_n = \left( n - 1 \right) \bigl({!\left( n - 1 \right)} + {!\left( n - 2 \right)}\bigr) for n \geq 2, where !0 = 1 and !1 = 0. The number of derangements of small lengths is given in the table below. There are various other expressions for D_n, equivalent to the formula given above. These include D_n = n! \sum_{i=0}^n \frac{(-1)^i}{i!} for n \geq 0 and :D_n = \left[ \frac{n!}{e} \right] = \left\lfloor\frac{n!}{e}+\frac{1}{2}\right\rfloor for n \geq 1, where \left[ x\right] is the
nearest integer function and \left\lfloor x \right\rfloor is the
floor function. D_n = \left\lfloor \frac{n!+1}{e} \right\rfloor,\quad\ n \ge 1, D_n = \left\lfloor \left(e + e^{-1}\right)n!\right\rfloor - \lfloor en!\rfloor,\quad n \geq 2, and D_n = n! - \sum_{i=1}^n {n \choose i} \cdot {!(n - i)},\quad\ n \ge 1. The following recurrence also holds: ==Growth of number of derangements as
n approaches ∞==