Permutations/Rank of a permutation: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
(Added Quackery.)
Line 1,820: Line 1,820:
result += s * fact(i)
result += s * fact(i)
return result</syntaxhighlight>
return result</syntaxhighlight>

=={{header|Quackery}}==

Stretch goal: The words defined here would handle the limitations of the Stack Exchange question with ease, as Quackery numbers are BigInts. The only inconvenience would be generating random numbers in the range 0 to 144! - 1, as the built-in PRNG is limited to 64 bits.

<syntaxhighlight lang="Quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )

[ [] swap times [ i^ join ] ] is identity ( n --> [ )
[ [] unrot 1 - times
[ i 1+ ! /mod
dip join ] drop ] is factoradic ( n n --> [ )

[ [] unrot witheach
[ pluck
rot swap nested join
swap ]
join ] is inversion ( [ [ --> [ )
[ dup identity unrot
factoradic inversion ] is rank->perm ( n n --> [ )
[ 0 over size identity rot
witheach
[ over find
dup dip [ pluck drop ]
rot i 1+ * + swap ]
drop ] is perm->rank ( [ --> n )

3 ! times
[ i^
dup echo say " -> "
3 rank->perm
dup echo say " -> "
perm->rank
echo cr ]
cr
4 times
[ 12 ! random
dup echo say " -> "
12 rank->perm
dup echo say " -> "
perm->rank
echo cr ]</syntaxhighlight>

{{out}}

<pre>0 -> [ 0 1 2 ] -> 0
1 -> [ 0 2 1 ] -> 1
2 -> [ 1 0 2 ] -> 2
3 -> [ 1 2 0 ] -> 3
4 -> [ 2 0 1 ] -> 4
5 -> [ 2 1 0 ] -> 5

355561581 -> [ 8 10 11 7 3 9 4 0 6 2 5 1 ] -> 355561581
297516255 -> [ 7 4 11 9 10 0 1 2 6 5 8 3 ] -> 297516255
196469317 -> [ 4 11 1 5 9 0 2 6 8 3 10 7 ] -> 196469317
309764555 -> [ 7 9 3 6 8 1 5 2 4 11 10 0 ] -> 309764555
</pre>


=={{header|Racket}}==
=={{header|Racket}}==