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 (2) 

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 (2) 

The number of lists grows with n!/e:

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]=

Publisher

George Beck

Version History

  • 1.0.0 – 29 May 2019

Related Resources

License Information