Wolfram Research

Function Repository Resource:

Derangements

Source Notebook

Give all the ways the numbers 1, 2, …, n can be scrambled completely

Contributed by: Wolfram Staff

ResourceFunction["Derangements"][n]

gives all lists that are rearrangements of {1,2,,n} such that no element appears in its original position.

Examples

Basic Examples

In the following two lists, no element appears in its original place:

In[1]:=
ResourceFunction["Derangements"]@3
Out[1]=

Here is the next case, condensed:

In[2]:=
Row /@ ResourceFunction["Derangements"]@4
Out[2]=

Scope

The number of lists grows with n!/:

In[3]:=
7!/E - Length@ResourceFunction["Derangements"]@Range@7 // N
Out[3]=

In fact, the number of derangements is given by the built-in subfactorial function:

In[4]:=
Subfactorial@7 == Length@ResourceFunction["Derangements"]@Range@7
Out[4]=

Resource History

See Also

License Information