How to use it
- Enter the number of items n.
- Optionally enter k to count the permutations with exactly k items in place.
- Read !n, n! and the probabilities. Large values are shortened on screen; the copy button copies all the digits.
How it works
The calculator builds !0, !1, … !n exactly with whole-number arithmetic using !m = (m − 1)(!(m − 1) + !(m − 2)). The probability of no fixed point is !n/n!. Permutations with exactly k fixed points are C(n, k)·!(n − k), because you choose the k items that stay and derange the other n − k.
Worked example
n = 5: the sequence runs 1, 0, 1, 2, 9, 44, so !5 = 44 out of 5! = 120 permutations. The probability that no item stays in place is 44/120 = 0.3666666667, and the probability that at least one does is 0.6333333333.
With k = 1: C(5, 1)·!4 = 5 · 9 = 45 permutations have exactly one item in place, probability 45/120 = 0.375.
Tips and common mistakes
- !n is the subfactorial, not “n factorial”: it is written with the exclamation mark in front.
- For n = 1 there is no derangement (!1 = 0); for n = 0 the empty arrangement counts as one (!0 = 1).
- For a gift exchange where nobody draws themselves, the draw is fair only if every derangement is equally likely; starting the whole draw over whenever someone gets their own name does that.