Subfactorial Calculator

Find the number of derangements of n items.

How it is calculated

The subfactorial !n counts derangements: arrangements of n distinct items in which no item ends up in its original position. The tool uses the recurrence !n = (n − 1) × (!(n − 1) + !(n − 2)) with !0 = 1 and !1 = 0. Equivalently, !n is the nearest whole number to n! / e.

n is cut to a whole number and must be between 0 and 170. Values grow quickly, and above roughly n = 18 the intermediate numbers exceed what ordinary floating-point arithmetic holds exactly, so only the leading digits of very large results can be trusted. For n = 1 the answer is 0, because a single item cannot be moved.

Worked example

For n = 5: !0 = 1, !1 = 0. !2 = 1 × (0 + 1) = 1. !3 = 2 × (1 + 0) = 2. !4 = 3 × (2 + 1) = 9. !5 = 4 × (9 + 2) = 44. Check: 5! / e = 120 / 2.71828 ≈ 44.15, nearest whole number 44. The tool shows 44.

Try this example →

Common mistakes

  • Confusing !n with n!: for 5 they are 44 and 120.
  • Thinking !n counts permutations where at least one item moves; that would be n! − 1. A derangement needs every item to move.
  • Reading "!5" as a factorial from other notation; here the exclamation mark comes first.
  • Trusting every digit of the answer for big n; results above about n = 18 are approximate.

About this calculator

The subfactorial !n, also called the number of derangements of n, counts the permutations of n objects in which no object ends up in its original position. It's defined by !n = n! · Σ(k=0 to n) (−1)^k/k!, and grows very close to n!/e for large n — in fact !n is the nearest integer to n!/e. For example !4 = 9: out of the 24 permutations of 4 items, only 9 leave every item out of place.

The classic illustration is the "hat-check problem" (or "misaddressed letters problem"): if n people check their hats and they're returned randomly, !n is the number of ways every single person gets the wrong hat back. Combinatorics students, probability courses and puzzle enthusiasts use subfactorial calculations to solve derangement problems, and the concept extends into cryptography and coding theory wherever "nothing stays in its original slot" permutations matter.

Was this helpful?

You can pick up to 3 reactions.

Comments (1)

  • Editorial team

    A useful sanity check is the ratio !n divided by n!. It settles near 0.368, which is 1/e, almost immediately: for n = 5 it is 44/120, about 0.367. So a random shuffle of even a few items leaves nobody in place roughly one time in three. This is the maths behind secret Santa draws where no one may pick their own name. Which derangement puzzle brought you here?

Popular calculators

All Calculators