Jump to content

Permutations/Derangements: Difference between revisions

Add Factor example
(Add Factor example)
Line 1,126:
20 : 895014631192902121
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.98}}
<lang factor>USING: combinators formatting io kernel math math.combinatorics
math.factorials prettyprint sequences ;
IN: rosetta-code.derangements
 
: !n ( n -- m )
{
{ 0 [ 1 ] }
{ 1 [ 0 ] }
[ [ 1 - !n ] [ 2 - !n + ] [ 1 - * ] tri ]
} case ;
 
: derangements ( n -- seq )
<iota> dup [ [ = ] 2map [ f = ] all? ] with
filter-permutations ;
 
"4 derangements" print 4 derangements . nl
"n count calc\n= ====== ======" print
10 <iota> [
dup [ derangements length ] [ !n ] bi
"%d%8d%8d\n" printf
] each nl
"!20 = " write 20 !n .</lang>
{{out}}
<pre>
4 derangements
V{
{ 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 }
}
 
n count calc
= ====== ======
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|FreeBASIC}}==
<lang freebasic>' version 08-04-2017
1,808

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.