29 lines
1.1 KiB
Plaintext
29 lines
1.1 KiB
Plaintext
A [http://mathworld.wolfram.com/Derangement.html derangement] is a permutation of the order of distinct items in which ''no item appears in its original place''.
|
|
|
|
For example, the only two derangements of the three items (0, 1, 2) are (1, 2, 0), and (2, 0, 1).
|
|
|
|
The number of derangements of ''n'' distinct items is known as the subfactorial of ''n'', sometimes written as !''n''.
|
|
There are various ways to [[wp:Derangement#Counting_derangements|calculate]] !''n''.
|
|
|
|
|
|
;Task:
|
|
# Create a named function/method/subroutine/... to generate derangements of the integers ''0..n-1'', (or ''1..n'' if you prefer).
|
|
# Generate ''and show'' all the derangements of 4 integers using the above routine.
|
|
# Create a function that calculates the subfactorial of ''n'', !''n''.
|
|
# Print and show a table of the ''counted'' number of derangements of ''n'' vs. the calculated !''n'' for n from 0..9 inclusive.
|
|
|
|
|
|
;Optional stretch goal:
|
|
* Calculate <big><big> !''20'' </big></big>
|
|
|
|
|
|
;Related tasks:
|
|
* [[Anagrams/Deranged anagrams]]
|
|
* [[Best shuffle]]
|
|
* [[Left_factorials]]
|
|
|
|
|
|
{{Template:Strings}}
|
|
<br><br>
|
|
|