RosettaCodeData/Task/Permutations-Derangements/11l/permutations-derangements.11l
2026-02-01 16:33:20 -08:00

24 lines
571 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) 0.<10
print(#2 #<6 #..format(n, derangements(n).len, subfact(n)))
n = 20
print("\n!#. = #.".format(n, subfact(n)))