Warning, this post uses some fierce math to prove why the pattern continues in the last post. If you want to know why the pattern continues, then welcome to the explanation.

In the last post we counted how many ways to hand student back their tests so that no student had his or her own test. This type of matching has a special name, a derangement. For a group of *n* students we denote the derangement as

!*n*

To determine this quantity we need a formula. From before, we know that !3 = 2 and !4 = 9. Now consider the general case. How many ways are there of handing back a test to the first student? We certainly can’t give him his own test. However, we could give him any of the other n – 1 tests. Let’s say we give him test *i.*

We could hand out the next test to anyone. Let’s consider handing it out to student *i* (the student with test *i*). We have two options. First, student *i* gets student 1’s test. Here is a picture:

Once these 2 students have been assigned their tests, we have n – 2 students who still need tests. However, assigned them tests is analogous to our original problem, and we can denote this as !(n – 2).

Second, student *i* does not get student 1’s test. Thus, student *i* has 1 forbidden test. All the other students also have 1 forbidden test (namely their own test). Again, this is analogous to our original problem with n – 1 students (since we have not given student *i* a test yet). We can denote this as !(n – 1).

To summarize, to hand out n tests, we have the following recursive formula:

We can use this formula to calculate derangements as high as we like, provided we proceed in order.

!5 = 4(!4 + !3) = 4(9 + 2) = 44

!6 = 5(!5 + !4) = 5(44 + 9) = 265

These are exactly the numerators we found when computing the fractions in the previous post!

To determine what would happen in a large class, we need a closed form for derangements. To simplify the notation for the next section, we will denote !n as D(n).

Observe the following pattern:

D(n) |
D(n)-nD(n-1) |

D(4) = 9 |
9 – 4*2 = 1 |

D(5) = 44 |
44 – 5*9 = -1 |

D(6) = 265 |
265 – 6*44 = 1 |

D(7) = 1854 |
1854 – 7*265 = -1 |

We can prove the above pattern continues using induction. The base case has been dealt with above. Our inductive hypothesis is:

Consider n+1:

using the recurrence relation

by the inductive hypothesis

Using this information, we have a new recurrence relation for D(n):

Unfortunately, this is still not a closed form solution. However, we will use induction again to get us the rest of the way. The following is the closed form solution for D(n):

Checking the OEIS, we find that D(0) = 1 and D(1) = 0. Using the formula for D(3), we can see that the base case will hold:

as expected.

Suppose the formula is true for D(n). Consider D(n+1):

from the new recursive formula

using the inductive hypothesis

definition of factorial

Thus, the formula is true for any n. This formula is closed, but complicated. The last step in the process is to relate derangements to our original problem. Since we were looking for a probability, we were really interested in . Using the Taylor series approximation for e^{-1} we get the following:

Finally! We have our answer! Since the number of students determines how many terms of the sequence for e we use, the approximation is valid for even small classes of only 10 or 15 students. The probability of handing out tests properly is approximately