Derangement icon Derangement Counter

Introduction to Derangements

This derangement counter measures how many permutations leave every labeled item out of its original place. For a set of n objects, the answer is the subfactorial !n, the count of rearrangements with zero fixed points. That makes the page useful for Secret Santa assignments, envelope mismatches, seating swaps, and any puzzle where nothing may stay where it began.

The useful comparison is between all permutations and the smaller set of derangements. There are n! total reorderings, but only some satisfy the no-fixed-point rule. For small inputs you can list them by hand; for larger inputs the count grows far too quickly, which is why a dedicated calculator is so convenient. This page reports the derangement count, the total permutation count, and the ratio between them so you can judge how restrictive the rule really is.

Derangements also have a famous long-run pattern. As n grows, the fraction of all permutations that qualify as derangements approaches 1/e, about 0.367879. In practical terms, a large random shuffle has a little under a 37% chance of leaving every item out of its starting spot.

How to Use This Derangement Counter

Enter the number of elements n in the form below and click Calculate Derangements. The input must be a non-negative integer from 0 to 170. After you submit, the page shows three pieces of information: the derangement count !n, the total number of permutations n!, and the ratio !n/n!, together with the benchmark 1/e.

Read the result in the context of your own problem. If you are planning a gift exchange, !4 = 9 means there are nine complete assignment patterns in which nobody keeps their own gift. If you are thinking about probability, divide the derangement count by the total number of permutations. That ratio is the chance that a random arrangement produces no fixed points, and it is often easier to interpret than the raw count because factorials grow so quickly.

One practical note is worth keeping in mind. The calculator accepts values up to 170 because standard JavaScript numbers can still represent values in that range without overflowing to infinity. For large inputs, though, the displayed counts are floating-point values, so once the true integer exceeds the JavaScript safe-integer range, the exact final digits are no longer guaranteed. Small and moderate inputs are exact; larger inputs should be read as reliable magnitudes rather than arbitrary-precision arithmetic.

Derangement Formula

The calculator uses the standard recurrence for derangements, which builds !n from the two previous counts. Once one item has been placed, the remaining arrangement splits into the familiar two-case structure that makes this recurrence work.

!n=(n1)(!(n1)+!(n2))

The base cases matter just as much as the recurrence. We define !0 = 1 because the empty arrangement has no fixed points, and !1 = 0 because a single item has nowhere else to go. From there the sequence builds upward: !2 = 1, !3 = 2, !4 = 9, !5 = 44, and so on. That is the method used by the calculator script because it is simple, fast, and numerically stable for browser use.

There is also a famous approximation involving e. The derangement count is the nearest integer to n!/e.

!n=n!e+12

This approximation is handy because it explains why the ratio !n/n! settles near 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 until only no-fixed-point arrangements remain. The closed sum is:

!n=n!k=0n(1)kk!

In plain language, the alternating sum keeps correcting the overcount from the previous step until only true derangements are left. 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 in Plain Language

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: Derangements of Four Items

To see the derangement counter in action, imagine four gifts labeled A, B, C, and D, with nobody allowed to keep the item that started in their own spot. 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 show how quickly the derangement ratio approaches its limiting value.

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.

Where Derangements Show Up

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.

Derangement Ratio Comparison Table

Growth of derangements compared to total permutations
nn! (Permutations)!n (Derangements)Ratio (!n / n!)
0111.000000
1100.000000
2210.500000
3620.333333
42490.375000
5120440.366667
67202650.368056
75,0401,8540.367857
840,32014,8330.367882
9362,880133,4960.367879
103,628,8001,334,9610.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 of the Derangement Problem

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.

Computing Derangements in the Browser

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.

Related Derangement Variants

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 Interpreting Derangement Results

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 for Derangement Counts

  • 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 for derangements.
  • 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 About Derangements

Why is !0 = 1? For derangements, the empty set counts as one valid arrangement because the empty permutation has no fixed points and the recurrence needs a starting value.

Can I use negative or non-integer values? No. Derangements count rearrangements of whole labeled items, so the input must be a non-negative integer.

Why does the answer look like scientific notation for large n? Because derangement counts grow very quickly. The calculator switches to exponential formatting once the numbers become large enough that commas are not enough.

Is the ratio still useful when the raw count is huge? Yes. The ratio is often the most informative output in derangement problems because it stabilizes near 1/e while the raw count keeps exploding.

What is the largest input here? The form accepts 170 so the browser arithmetic stays below overflow to infinity.

Further Exploration with Derangements

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.

Enter a non-negative integer from 0 to 170.

Enter the number of elements to calculate derangements.

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.

Score0
Time75.0s
Streak0
Wave1
Completed0

Derangement Dispatcher

Route each numbered gift into a different numbered home. Finish complete no-fixed-point waves before the timer runs out.

  • Tap or click a home slot, or use the arrow keys and press Enter.
  • Never send label 3 to Home 3, label 4 to Home 4, and so on.
  • Complete the whole wave to bank a derangement bonus and extra time.

Best score: 0

Educational takeaway: a derangement counts only when every single label avoids its original home at the same time.

Embed this calculator

Copy and paste the HTML below to add the Derangement Counter - Count No-Fixed-Point Permutations to your website.