Permutations/Derangements: Difference between revisions
Content added Content deleted
(Added Wren) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 28: | Line 28: | ||
{{Template:Strings}} |
{{Template:Strings}} |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>F derangements(n) |
|||
[[Int]] r |
|||
V perm = Array(0 .< n) |
|||
L |
|||
I all(enumerate(perm).map((indx, p) -> indx != p)) |
|||
r [+]= perm |
|||
I !perm.next_permutation() |
|||
L.break |
|||
R r |
|||
F subfact(n) -> Int64 |
|||
R I n < 2 {1 - n} E (subfact(n - 1) + subfact(n - 2)) * (n - 1) |
|||
V n = 4 |
|||
print(‘Derangements of ’Array(0 .< n)) |
|||
L(d) derangements(n) |
|||
print(‘ ’d) |
|||
print("\nTable of n vs counted vs calculated derangements") |
|||
L(n) 10 |
|||
print(‘#2 #<5 #<5’.format(n, derangements(n).len, subfact(n))) |
|||
n = 20 |
|||
print("\n!#. = #.".format(n, subfact(n)))</lang> |
|||
{{out}} |
|||
<pre> |
|||
Derangements of [0, 1, 2, 3] |
|||
[1, 0, 3, 2] |
|||
[1, 2, 3, 0] |
|||
[1, 3, 0, 2] |
|||
[2, 0, 3, 1] |
|||
[2, 3, 0, 1] |
|||
[2, 3, 1, 0] |
|||
[3, 0, 1, 2] |
|||
[3, 2, 0, 1] |
|||
[3, 2, 1, 0] |
|||
Table of n vs counted vs calculated derangements |
|||
0 1 1 |
|||
1 0 0 |
|||
2 1 1 |
|||
3 2 2 |
|||
4 9 9 |
|||
5 44 44 |
|||
6 265 265 |
|||
7 1854 1854 |
|||
8 14833 14833 |
|||
9 133496 133496 |
|||
!20 = 895014631192902121 |
|||
</pre> |
|||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |