Derangement

DOWNLOAD Mathematica Notebook

A derangement is a permutation in which none of the objects appear in their "natural" (i.e., ordered) place. For example, the only derangements of {1,2,3} are {2,3,1} and {3,1,2}, so !3=2. Similarly, the derangements of {1,2,3,4} are {2,1,4,3}, {2,3,4,1}, {2,4,1,3}, {3,1,4,2}, {3,4,1,2}, {3,4,2,1}, {4,1,2,3}, {4,3,1,2}, and {4,3,2,1}. Derangements are permutations without fixed points (i.e., having no cycles of length one). The derangements of a list of n elements can be computed in the Wolfram Language using

Derangements[l_List] := With[
  {perms = Permutations[l]},
  {supp = PermutationSupport /@ perms},
  Pick[perms, Length /@ supp, Length[l]]
]

The derangement problem was formulated by P. R. de Montmort in 1708, and solved by him in 1713 (de Montmort 1713-1714). Nicholas Bernoulli also solved the problem using the inclusion-exclusion principle (de Montmort 1713-1714, p. 301; Bhatnagar 1995, p. 8).

Derangements are also called rencontres numbers (named after rencontres solitaire) or complete permutations, and the number of derangements !n on n elements is called the subfactorial of n.

The function giving the number of distinct derangements on n elements is called the subfactorial !n and is equal to

!n=n!sum_(k=0)^(n)((-1)^k)/(k!)
(1)
=(Gamma(n+1,-1))/e
(2)

(Bhatnagar 1995, pp. 8-9), where Gamma(z,a) is the incomplete gamma function, or

 !n=[(n!)/e],
(3)

where k! is the usual factorial and [x] is the nearest integer function.

As the number of objects increases, the probability P that none appears in its correct position approaches

 P=lim_(n->infty)(!n)/(n!)=1/e
(4)

(Wells 1986, p. 27). In fact, the probability is very close to 1/e already even for as few as four points.

The number of derangements !n=d(n) of length n satisfy the recurrence relations

 d(n)=(n-1)[d(n-1)+d(n-2)]
(5)

and

 d(n)=nd(n-1)+(-1)^n,
(6)

with d(1)=0 and d(2)=1 (Skiena 1990, p. 33). The first few are 0, 1, 2, 9, 44, 265, 1854, ... (OEIS A000166). The recurrence can be solved exactly, although the sequence of d(n) cannot be expressed as a fixed number of hypergeometric terms (Petkovšek et al. 1996, pp. 157-160; Koepf 1998, p. 159).

Wolfram Web Resources

Mathematica »

The #1 tool for creating Demonstrations and anything technical.

Wolfram|Alpha »

Explore anything with the first computational knowledge engine.

Wolfram Demonstrations Project »

Explore thousands of free applications across science, mathematics, engineering, technology, business, art, finance, social sciences, and more.

Computerbasedmath.org »

Join the initiative for modernizing math education.

Online Integral Calculator »

Solve integrals with Wolfram|Alpha.

Step-by-step Solutions »

Walk through homework problems step-by-step from beginning to end. Hints help you try the next step on your own.

Wolfram Problem Generator »

Unlimited random practice problems and answers with built-in Step-by-step solutions. Practice online or make a printable study sheet.

Wolfram Education Portal »

Collection of teaching and learning tools built by Wolfram education experts: dynamic textbook, lesson plans, widgets, interactive Demonstrations, and more.

Wolfram Language »

Knowledge-based programming for everyone.