Permutations/Derangements: Difference between revisions

Added Arturo implementation
(Added Arturo implementation)
Line 446:
9 133496 133496
!20 = 895014631192902121</pre>
 
=={{header|Arturo}}==
 
<lang rebol>isClean?: function [s,o][
loop.with:'i s 'a [
if a = o\[i] -> return false
]
return true
]
 
derangements: function [n][
original: 1..n
select permutate original 'x ->
isClean? x original
]
 
subfactorial: function [n].memoize[
(n =< 1)? -> 1 - n
-> (n-1) * (add subfactorial n-1 subfactorial n-2)
]
 
print "Derangements of 1 2 3 4:"
loop derangements 4 'x [
print x
]
 
print "\nNumber of derangements:"
print [pad "n" 5 pad "counted" 15 pad "calculated" 15]
print repeat "-" 39
loop 0..9 'z [
counted: size derangements z
calculated: subfactorial z
print [pad to :string z 5 pad to :string counted 15 pad to :string calculated 15]
]
 
print ~"\n!20 = |subfactorial 20|"</lang>
 
{{out}}
 
<pre>Derangements of 1 2 3 4:
4 1 2 3
3 1 4 2
3 4 1 2
4 3 1 2
2 1 4 3
2 4 1 3
2 3 4 1
3 4 2 1
4 3 2 1
 
Number of derangements:
n counted calculated
---------------------------------------
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|AutoHotkey}}==
1,532

edits