Skip to main content

Derangement (Subfactorial) Calculator

Calculate !n, the number of derangements of n elements — permutations that leave no item in its original position. Returns the exact count, n!, and the derangement probability, computed with arbitrary-precision arithmetic.

Input

Output

Result
MetricValue
No data yet
Step-by-Step Solution
Was this helpful?

More ways to use this tool

REST API

curl -X POST https://api.iotools.cloud/v1/tool/derangement-calculator \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "n": "5"
  }'

Swap in your own key from your account. The tool's fields are the body — no wrapper.

Ask an AI agent

Use the IOTools `derangement-calculator` tool (Derangement (Subfactorial) Calculator) on this input:

YOUR_INPUT_HERE

Paste this at any agent connected to the IOTools MCP server, then add your input.

Embed widget

<iframe
  src="https://iotools.cloud/embed/derangement-calculator/"
  width="100%" height="520" frameborder="0" scrolling="no" loading="lazy"
  title="Derangement (Subfactorial) Calculator — iotools.cloud"
  sandbox="allow-scripts allow-forms allow-same-origin allow-downloads allow-popups allow-popups-to-escape-sandbox"
  allow="clipboard-write"
  style="width:100%;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden"></iframe>
<script src="https://iotools.cloud/embed.js" async></script>

Drop this into your own page — free, no key required, just a link back.

Cost per API/MCP callFrom 5 credits
Need more credits?View pricing

Also available with

Guides

The Derangement (Subfactorial) Calculator computes !n, the number of derangements of n elements — permutations of a set that leave every element out of its original position. Enter n and it instantly returns the exact count, the total number of permutations (n!), and the derangement probability (!n / n!), computed with arbitrary-precision arithmetic so results stay exact even for large n.

It's built for combinatorics students, teachers preparing worked examples on the classic "hat-check problem," and anyone who needs an exact derangement count without hand-cranking factorials.

How to use it

  1. Enter n, the number of elements (0 or greater).
  2. Read the Derangements !n row for the count, Total permutations n! for the full factorial, and Probability (!n / n!) for the fraction of permutations with no fixed points.
  3. Step-by-Step Solution shows the recurrence used to build up the answer.

Results update automatically as you type. For example, entering n = 5 returns !5 = 44 out of 5! = 120 total permutations — a probability of about 0.3667.

What is a derangement?

A derangement is a permutation where no element ends up in its own original position. The name comes from the classic "hat-check problem": if n people check their hats and the hats are returned at random, a derangement is an outcome where nobody gets their own hat back.

How is !n calculated?

This calculator uses the standard recurrence relation, built up iteratively from the base cases:

!0 = 1
!1 = 0
!n = (n − 1) × (!(n − 1) + !(n − 2))    for n ≥ 2

This is equivalent to the closed-form sum !n = n! × Σ(k=0 to n) (−1)^k / k!, but the recurrence avoids floating-point rounding entirely — every intermediate value is an exact integer.

Why does the probability approach 1/e?

As n grows, the derangement probability !n/n! converges to 1/e ≈ 0.367879 — a surprisingly fast convergence; by n = 10 the probability already matches 1/e to six decimal places. This means that no matter how large the hat-check line gets, there's roughly a 37% chance nobody gets their own hat back.

Need permutations or combinations instead — arrangements that don't have the no-fixed-point restriction? See the Permutation & Combination Calculator, which computes nPr and nCr from the same n! building block.

Privacy

This calculator runs entirely in your browser. Your input is never uploaded, logged, or stored — the computation happens locally on your device.

derangementsubfactorialhat check problempermutationcombinatoricsno fixed pointsprobabilitymath

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.