Derangement Counter
Introduction
This calculator counts derangements: permutations in which no item stays in its original position. If you have n labeled objects and you rearrange them so that every object moves somewhere else, the number of valid rearrangements is written as !n, read as subfactorial n. That simple idea appears in many classic puzzles. It is the right tool when you ask questions such as: how many Secret Santa assignments avoid self-gifts, how many envelopes can hold the wrong letters, or how many shuffled items end up with no exact matches to where they started.
What makes derangements memorable is the tension between two counts. The total number of possible rearrangements is n!, but only some of those are valid no-fixed-point arrangements. For small sets, you can list them by hand. For larger sets, the count grows very quickly, and the calculator becomes the practical way to see the answer. The page below gives the derangement count, the total number of permutations, and the ratio between them, which helps you compare the strict no-fixed-point condition to all possible permutations.
Derangements are one of the nicest meeting points between counting and probability. They come from discrete combinatorics, yet their long-run behavior connects to the constant e. As n gets large, the proportion of permutations that are derangements approaches 1/e, about 0.367879. In plain language, for a large random shuffle there is roughly a 36.8% chance that nothing lands in its original place.
How to Use
Enter the number of elements n in the form below and click Calculate Derangements. The input should be a non-negative integer from 0 to 170. After you submit, the page shows three pieces of information. First, it shows the derangement count !n. Second, it shows the total number of permutations n!. Third, it shows the ratio !n/n!, alongside the benchmark 1/e.
Interpret the result according to your problem. If you are doing a gift exchange with four people and the result says !4 = 9, that means there are nine different complete assignment patterns in which nobody keeps their own gift. If you are studying probability, divide the derangement count by the total number of permutations. That ratio is the chance that a random arrangement produces no fixed points. The ratio is often more intuitive than the raw count because factorials grow so fast.
One practical note is worth knowing. This page accepts values up to 170 because standard JavaScript numbers can still represent the overall size of factorials in that range without overflowing to infinity. However, very large counts are displayed using floating-point arithmetic, so once the true integer becomes larger than the JavaScript safe-integer range, the exact last digits are no longer guaranteed. For small and moderate inputs the integer results are exact; for larger inputs, read the scientific-notation output and ratio as high-level numerical approximations rather than arbitrary-precision integer arithmetic.
Formula
The calculator is built around the standard recurrence for derangements. The recurrence says that to derange n items, you can focus on where one chosen item goes, then count the two ways the remaining structure can unfold. That logic leads to a compact formula that depends only on the two previous derangement counts.
The base cases are just as important as the recurrence. We take !0 = 1 because the empty arrangement has no fixed points, and !1 = 0 because a single item has no way to move away from itself. From there, the recurrence builds the count step by step: !2 = 1, !3 = 2, !4 = 9, !5 = 44, and so on. That is the method used by the calculator script because it is fast, stable, and easy to understand.
There is also a famous approximation and rounding rule involving e. It says the derangement count is the nearest integer to n!/e.
This is useful for intuition because it shows why the ratio !n/n! gets close to 1/e so quickly. The exact counting formula comes from inclusion-exclusion. Start with all permutations, subtract those with at least one fixed point, add back those with at least two fixed points, and continue alternating. The exact closed sum is:
If you want a plain-language reading of that summation, it alternates between removing and restoring overcounted cases until only the arrangements with no fixed points remain. The recurrence is usually the easiest way to compute values, while the inclusion-exclusion form is the clearest explanation of why the count has that shape.
Understanding Derangements
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 the classic gift-exchange problem and the envelope-letter mismatch problem. This fundamental concept appears in probability theory, cryptography, scheduling, and teaching examples for the inclusion-exclusion principle.
The number of derangements of n elements is denoted !n. It grows rapidly with n, but always stays below 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, so a little over one third of all permutations are complete no-fixed-point rearrangements.
Worked Example
Suppose four friends, Alice, Bob, Carol, and Dave, are exchanging gifts and nobody may receive their own gift. We need the number of derangements of four elements, written !4. Start from the base cases !0 = 1 and !1 = 0. Then compute upward:
- !2 = (2−1)(!(2−1) + !(2−2)) = 1(0 + 1) = 1
- !3 = (3−1)(!(3−1) + !(3−2)) = 2(1 + 0) = 2
- !4 = (4−1)(!(4−1) + !(4−2)) = 3(2 + 1) = 9
So there are exactly 9 valid gift-exchange arrangements. Since the total number of assignments is 4! = 24, the probability that a random assignment is valid is 9/24 = 0.375. That is already close to 1/e, which helps explain why derangements are often taught as a bridge from combinatorics to probability.
You can also interpret the same example as a seating chart, a stack of letters and envelopes, or a randomized quality-control reassignment where every item must move to a different destination than where it started. The underlying counting problem is identical: count permutations with no fixed points.
Common Applications
Derangements are more than a classroom curiosity. They model any situation where every participant or item must be reassigned away from its original destination. A few common examples are listed below, but the important pattern is the same in each case: you are counting complete reassignments, not just partial mismatches.
- Secret Santa and gift exchanges: nobody receives their own gift.
- Hat-check and coat-check puzzles: nobody gets the correct hat or coat back.
- Letter-envelope matching: each letter goes to the wrong envelope.
- Card and deck problems: after a shuffle, no card remains in its original position.
- Assignment and scheduling models: tasks, seats, or jobs must all move away from their original owners.
- Probability exercises: the ratio !n/n! gives the chance of a full mismatch.
When the scenario has additional restrictions, such as some assignments being forbidden for extra reasons or some matches being allowed, derangements alone are not enough. In that case you move into the broader world of restricted permutations. This calculator is specifically for the clean no-fixed-point version.
Derangements vs. Permutations: Comparison Table
| 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 settles near 1/e. By the time you reach n = 10, the proportion is already stable to several decimal places. That fast convergence is one reason the hat-check problem remains so popular in textbooks and probability courses.
Historical Context
The derangement problem has roots in eighteenth-century probability. Pierre Rémond de Montmort studied matching problems in games of chance, and Leonhard Euler later helped popularize elegant solutions. The problem became famous through the hat-check story: if a distracted attendant returns hats at random, what is the chance that nobody receives the correct one? The answer tends toward 1/e, which is striking because a discrete counting problem ends up tied to a constant that students usually meet in calculus.
This history matters because it explains why derangements still show up so often in teaching. They are simple enough to state in one sentence, rich enough to motivate inclusion-exclusion, and surprising enough to be memorable. That combination is rare in mathematics.
Computational Considerations
Both factorials and derangements grow extremely quickly. In a browser, the calculator uses standard JavaScript numbers, which are floating-point values rather than arbitrary-precision integers. That means the page can still represent the general magnitude of values up to 170 without overflowing, but the exact integer digits are only guaranteed while the result stays within the JavaScript safe-integer range. For larger inputs, the scientific-notation display is still useful for scale and for the ratio to 1/e, but it should not be treated as exact symbolic arithmetic.
The recurrence is still the right computational choice for a lightweight page because it runs in linear time and uses only a few running values. It is much more efficient than generating permutations directly, which would become impossible even for moderate n. If you need exact huge derangement counts for research or programming contests, you would switch to big integers or a symbolic math system.
Generalizations and Related Concepts
Derangements are the special case where the number of fixed points is exactly zero. A closely related question asks for the number of permutations with exactly k fixed points. To count those, choose which k elements stay put and derange the rest. That link shows that derangements sit inside a larger family of restricted permutation problems.
There are also partial derangements, rencontres numbers, and more elaborate avoidance problems in which certain positions are forbidden while others remain allowed. Those extensions appear in coding theory, combinatorial design, and advanced enumeration. If this calculator helps you build intuition for the no-fixed-point case, you are already standing at the doorway to those broader topics.
Practical Tips for Using This Calculator
Before using the result in a real problem, pause and confirm that your scenario truly requires a full mismatch. If even one person is allowed to keep their own assignment, or if some people have multiple acceptable destinations, the derangement formula no longer matches the real rules. In other words, derangements solve a very specific counting problem, and they solve it beautifully, but only when the setup fits.
For teaching or self-study, try a small input such as 3, 4, or 5 first. Compare the calculator result to a manual list of permutations. That habit makes the abstract formula much easier to trust, because you can see the no-fixed-point rule in concrete examples before moving to large values that are impossible to list by hand.
Limitations and Assumptions
- Integer input only: n must be a non-negative integer.
- Classical definition: the page uses !0 = 1 and !1 = 0, which is the standard convention.
- Uniform permutation model: the ratio !n/n! assumes every permutation is equally likely.
- Numerical precision: the browser accepts values up to 170, but large outputs are floating-point approximations rather than arbitrary-precision exact integers.
Frequently Asked Questions
Why is !0 = 1? The empty arrangement has no fixed points, so counting it as one derangement makes the recurrence work cleanly.
Can I use negative or non-integer values? No. In this context, derangements count discrete rearrangements of whole labeled items.
Why does the answer look like scientific notation for large n? Because the numbers become enormous. The page switches to exponential formatting for readability once the values are very large.
Is the ratio still useful when the raw count is huge? Yes. The ratio is often the most informative output, especially in probability questions, because it stabilizes quickly near 1/e.
What is the largest input here? The form accepts 170 to avoid overflow to infinity in ordinary browser arithmetic.
Further Exploration
If you want to go one step beyond the calculator, try computing the number of permutations with exactly two fixed points for n = 6, derive the inclusion-exclusion formula yourself, or compare the derangement probability for 10 items to the value of 1/e. Each of those exercises turns a button click into real combinatorial understanding.
Derangements reward both intuition and technique. The examples feel playful, the formulas are elegant, and the asymptotic behavior is genuinely surprising. That is a strong combination, and it is why this topic remains a favorite introduction to serious counting arguments.
Copy status messages appear here.
Mini-Game: Derangement Dispatcher
This optional mini-game turns the idea behind the calculator into a fast routing challenge. Each numbered gift must be sent to a different numbered home, so every completed wave is a full derangement. It does not change the calculator result at all; it is just a playful way to feel what the no-fixed-point rule means under pressure.
Educational takeaway: a derangement counts only when every single label avoids its original home at the same time.
