Derangement Calculator screenshot

Derangement Calculator

Count the permutations of n items that leave no item in its original place, exactly, for n up to 500.

Privacy First
0 Runs
Updated 2026-11-22
Runs in your browser
No sign-up, no upload
Exact where arithmetic allows

Derangement Calculator

Count the permutations of n items that leave no item in its original place, exactly, for n up to 500.

What the page prints, and what it refuses

Every figure is printed with the rule or formula that produced it, and any numerical tolerance is stated on the result rather than implied. The page refuses an input the formula does not define and names the value it read; it prints no answer it cannot support.

The calculation runs entirely in this browser tab; nothing is uploaded.

A derangement is a rearrangement in which nothing stays where it was. The page computes the subfactorial !n exactly: !5 is 44, because of the 120 permutations of five items only 44 have no fixed point.

The recurrence !n = (n − 1)(!(n − 1) + !(n − 2)) is evaluated with BigInt, so !500 is exact even though its 1134 digits overflow any double. The ratio !n ÷ n! approaches 1/e, and the page prints the ratio beside the count.

Why every derangement is built from a smaller one

Fix the first item. It must move to one of n − 1 places. If it swaps with item j there are !(n − 2) ways to finish; if it does not, the remaining items form a derangement of n − 1. That gives !n = (n − 1)(!(n − 1) + !(n − 2)).

The base cases are !0 = 1, the empty arrangement, and !1 = 0, because a single item has nowhere to move. The page states both rather than hiding them behind the loop.

How to use Derangement Calculator

Enter n as a whole number from 0 to 500, then press Run.

The first line is !n, followed by its digit count, n! and the ratio. For n = 0 or 1 the edge-case line explains the convention.

What to Expect

Best for

  • Counting arrangements with no fixed point
  • Checking a derangement exercise
  • Seeing how close !n ÷ n! comes to 1/e

Not ideal for

  • Counting arrangements with at least one fixed point, which is a different subtraction
  • n above 500
  • A factorial where a derangement is not meant

What this tool keeps

  • n exactly as entered

What may need cleanup

  • Nothing: !n is a final integer

Common errors

  • n above 500 is refused with the ceiling named
  • A fractional n is refused by name

Example use cases

  • Count the derangements of five items
  • Check that !4 is 9
  • Compare !n ÷ n! with 1/e

Sample input

n = 5

Sample output

!5 = 44, and !5 ÷ 5! = 0.3666666667

Who this is for

  • Students meeting inclusion–exclusion
  • Readers checking a combinatorics count

Frequently asked questions

What is a derangement?

A permutation that leaves no item in its original position. For three items the two derangements are the two three-cycles, so !3 is 2.

Why does the ratio approach 1/e?

The inclusion–exclusion sum for !n ÷ n! is 1 − 1/1! + 1/2! − … , which is the series for e⁻¹. The page prints the finite ratio, not the limit.

Is !0 really 1?

Yes, by the empty-product convention: there is exactly one arrangement of nothing, and it has no fixed point to violate. The page prints that reason.

Developer program

Build custom tools for your store

Join the SDK program to create useful ecommerce utilities for your store.