Derangements & Subfactorial
A derangement of {1, …, n} is a permutation in which no element appears in its original position — the "hat-check" problem. The number of derangements is the subfactorial !n: !0 = 1, !1 = 0, and !n = (n−1)·(!(n−1) + !(n−2)) for n ≥ 2 — equivalently !n = round(n! / e). This tool computes !n exactly (big integers), lists the first values, lists the actual derangements for small n, and shows the probability that a random permutation is a derangement, which tends to 1/e ≈ 0.3679. Runs locally in your browser.
Input
Result
Derangements
The subfactorial !n counts permutations of n items with no fixed point. The recurrence !n = (n−1)·(!(n−1) + !(n−2)) (with !0 = 1, !1 = 0) is the standard one; the closed form !n = n!·Σₖ₌₀ⁿ (−1)ᵏ / k! = round(n! / e) gives the same value. The probability !n / n! approaches 1/e ≈ 0.367879 — the chance that a randomly shuffled list has nothing in its original place. The listing is generated recursively and capped (derangements grow like n!/e, so only small n are listable). Pairs with the Combinatorics, Permutations, and Integer Partitions tools. Everything runs locally — nothing leaves your browser.