Derangement calculator (subfactorial !n)

How many ways can n letters go into n envelopes so that none lands in its own? Enter n to get the number of derangements !n exactly, the probability !n/n!, and how many permutations leave exactly k items in place.

A whole number from 0 to 1000.
Optional: how many items stay in their place, from 0 to n.
Derangements !n44
All permutations n!
120
Probability that no item stays in place
0.3666666667
Probability that at least one stays in place
0.6333333333
Permutations with exactly k fixed points
44
Probability of exactly k fixed points
0.3666666667
Limit 1/e for large n
0.3678794412
!0, !1, !2, …
1, 0, 1, 2, 9, 44

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Enter the number of items n.
  2. Optionally enter k to count the permutations with exactly k items in place.
  3. 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.

Frequently asked questions

What is a derangement?

A permutation in which no element is in its original position. For 1, 2, 3 there are two: 2, 3, 1 and 3, 1, 2. Their number is the subfactorial !n.

How is !n calculated?

By the recurrence !n = (n − 1)(!(n − 1) + !(n − 2)) with !0 = 1 and !1 = 0, or by inclusion–exclusion: !n = n! · Σ (−1)ᵏ/k! for k = 0…n. It is also the nearest whole number to n!/e for n ≥ 1.

What is the chance that nobody draws their own name in a gift exchange?

It is !n/n!, which is already 0.3666666667 for 5 people and gets closer to 1/e = 0.3678794412 as n grows. So in about 37 % of random draws nobody gets their own name.

What are rencontres numbers?

The number of permutations of n items with exactly k fixed points: C(n, k)·!(n − k). Choose which k stay, then derange the rest. For k = 0 it is !n.

More in Math

All tools in this category: Math ·Updated November 4, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.