A derangement is a permutation of a set where no element appears in its original position. Also known as a complete permutation or subfactorial, derangements answer questions like: "In how many ways can a group of people exchange gifts so that nobody receives their own gift?" or "How many ways can letters be placed in envelopes so that every letter goes to the wrong envelope?" This fundamental concept in combinatorics appears in probability theory, cryptography, secret Santa problems, and the famous hat-check problem.
The number of derangements of n elements is denoted !n (subfactorial n) and grows rapidly with n, though always remaining smaller than the total number of permutations n!. For example, with 3 items there are 3! = 6 total permutations, but only !3 = 2 derangements. As n increases, the ratio !n/n! approaches 1/e ≈ 0.36788, meaning roughly 36.788% of all permutations are derangements.
The subfactorial function can be calculated using several equivalent formulas. The most common recursive formula relates each derangement count to previous values:
This recursive relation tells us that the derangements of n elements equal (n−1) times the sum of derangements of (n−1) and (n−2) elements. The base cases are !0 = 1 (vacuously true: the empty permutation has no fixed points) and !1 = 0 (one element must stay in place). Alternatively, we can use the closed-form formula involving the nearest integer to n!/e:
Another exact formula uses the inclusion-exclusion principle summed over all possible overlaps:
This summation alternates between adding and subtracting terms, converging quickly for practical values of n. Our calculator uses the recursive formula for accuracy and efficiency, building up from base cases to handle integers up to 170 (the largest value for which results fit in standard floating-point precision).
Enter the number of elements n (between 0 and 170) in the input field and click "Calculate Derangements." The calculator will display the exact count of derangements !n, show you the inclusion-exclusion formula expansion, and provide the ratio !n/n! to demonstrate how close it is to 1/e. For small values of n, you'll see exact integer results. As n grows larger, the numbers become astronomical, but the ratio stabilizes near 0.367879441.
Suppose four friends—Alice, Bob, Carol, and Dave—participate in a gift exchange where nobody can receive their own gift. How many valid arrangements exist?
We need to find !4, the number of derangements of 4 elements. Using the recursive formula:
Therefore, there are exactly 9 derangements for 4 people. Since the total number of permutations is 4! = 24, the probability that a random arrangement is a derangement is 9/24 = 0.375, which is already very close to 1/e ≈ 0.368.
To verify, we can list all 9 valid derangements explicitly if we label positions 1,2,3,4 and people A,B,C,D:
Each arrangement ensures that A is not in position 1, B is not in position 2, C is not in position 3, and D is not in position 4. This manual enumeration confirms our calculation.
| n | n! (Permutations) | !n (Derangements) | Ratio (!n / n!) |
|---|---|---|---|
| 0 | 1 | 1 | 1.000000 |
| 1 | 1 | 0 | 0.000000 |
| 2 | 2 | 1 | 0.500000 |
| 3 | 6 | 2 | 0.333333 |
| 4 | 24 | 9 | 0.375000 |
| 5 | 120 | 44 | 0.366667 |
| 6 | 720 | 265 | 0.368056 |
| 7 | 5,040 | 1,854 | 0.367857 |
| 8 | 40,320 | 14,833 | 0.367882 |
| 9 | 362,880 | 133,496 | 0.367879 |
| 10 | 3,628,800 | 1,334,961 | 0.367879 |
Notice how quickly the ratio converges to approximately 0.367879, which equals 1/e. By n = 10, the ratio has stabilized to six decimal places.
The derangement problem has roots in 18th-century probability. Pierre Rémond de Montmort posed the matching problem in his 1708 work Essay d'analyse sur les jeux de hazard, and Leonhard Euler later provided elegant solutions. The subfactorial notation !n appeared in later mathematical literature to distinguish it from the standard factorial. The problem became famous as the "hat-check" or "coat-check" problem: a forgetful attendant returns n hats to n customers at random; what is the probability that nobody gets the correct hat?
Remarkably, even for just 10 people, the probability is already within 0.0001 of 1/e, and for large n the probability effectively becomes a constant. This counterintuitive result—that the probability doesn't depend significantly on group size once n is moderately large—makes derangements a favorite teaching example in discrete mathematics courses.
Calculating derangements for large n requires care because both n! and !n grow factorially. Standard floating-point arithmetic loses precision beyond n ≈ 170 because 171! exceeds the maximum representable value. Our calculator uses double-precision arithmetic and recursive computation to maintain accuracy within this range. For cryptographic or combinatorial applications requiring exact integer arithmetic for larger n, specialized arbitrary-precision libraries are necessary.
The recursive formula is efficient because it requires only two previous values, making the algorithm O(n) in time and O(1) in space (if we don't store intermediate results). The inclusion-exclusion sum is also O(n) but involves more arithmetic operations per step. For educational purposes, both approaches provide valuable insights into combinatorial reasoning.
Derangements are a special case of restricted permutations where we forbid certain positions for certain elements. More generally, the problème des rencontres asks for the number of permutations with exactly k fixed points. The formula for this involves binomial coefficients and derangements: choose which k elements stay fixed, then derange the remaining (n−k) elements.
Another generalization considers partial derangements or permutations avoiding specific patterns. Researchers in combinatorics study these using generating functions and the theory of symmetric functions. Derangements also connect to the Euler's constant e through the limit: lim (n→∞) !n/n! = 1/e, a beautiful example of how discrete structures relate to continuous analysis.
When solving real-world derangement problems, first verify that your scenario truly requires no fixed points. If partial matches are allowed (some people can receive their own gift), you need a different counting technique. The derangement formula applies strictly when all elements must be displaced.
For educational exercises, start with small values (n ≤ 5) and enumerate permutations by hand to build intuition. Then use the calculator to check larger cases. When presenting results, mention the ratio to 1/e to highlight the asymptotic behavior—it's a memorable fact that impresses audiences and reinforces the connection between combinatorics and calculus.
This calculator assumes:
Why is !0 = 1? By convention, the empty permutation (no elements) has no fixed points, so it is counted as one derangement. This choice makes formulas consistent and recursive relations work correctly.
Can I calculate derangements for non-integer or negative values? No. Derangements are defined only for non-negative integers because they count discrete permutation structures.
How accurate are the results? The calculator uses double-precision floating-point arithmetic, which is exact for integers up to 253 and provides high accuracy for factorials up to 170!. Beyond that, numerical overflow occurs.
What is the largest n I can compute? The calculator accepts n up to 170. Larger values cause overflow in standard floating-point representation. For exact results beyond this, use arbitrary-precision integer libraries.
How does this relate to the exponential constant e? The ratio !n/n! converges to 1/e as n increases. This is a classic result connecting discrete combinatorics to continuous mathematics.
To deepen your understanding of derangements, try these exercises:
Derangements are a gateway to richer topics in enumerative combinatorics, probability, and algebra. Enjoy discovering the patterns hidden in permutations!