Factorial base numbers indexing permutations of a collection: Difference between revisions

m
(Python example)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 10 users not shown)
Line 57:
A♠K♠Q♠J♠10♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥10♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦10♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣10♣9♣8♣7♣6♣5♣4♣3♣2♣
Finally create your own 51 digit factorial base number and produce the corresponding permutation of the above shoe
 
=={{header|AppleScript}}==
It's not clear from the description what part of the four subtasks "your function" is supposed to handle. It's also unclear whether "generate all permutaions of 11 digits" means "generate all 479,001,600 11-digit factorial base numbers" or "generate all permutations of an 11-integer array using the 39,916,800 10-digit factorial base numbers." However, both of the latter are out of the question with AppleScript.
 
<syntaxhighlight lang="applescript">-- Permutate a list according to a given factorial base number.
on FBNShuffle(|Ω|, fbn)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set fbnDigits to fbn's text items
set AppleScript's text item delimiters to astid
repeat with m from 1 to (count fbnDigits)
set m_plus_g to m + (item m of fbnDigits)
set v to item m_plus_g of |Ω|
repeat with i from (m_plus_g - 1) to m by -1
set item (i + 1) of |Ω| to item i of |Ω|
end repeat
set item m of |Ω| to v
end repeat
end FBNShuffle
 
-- Generate all the factorial base numbers having a given number of digits.
on generateFBNs(numberOfDigits)
script o
property partials : {}
property permutations : {}
end script
if (numberOfDigits > 0) then
repeat with i from 0 to numberOfDigits
set end of o's permutations to (i as text)
end repeat
repeat with maxDigit from (numberOfDigits - 1) to 1 by -1
set o's partials to o's permutations
set o's permutations to {}
repeat with i from 1 to (count o's partials)
set thisPartial to item i of o's partials
repeat with j from 0 to maxDigit
set end of o's permutations to (thisPartial & ("." & j))
end repeat
end repeat
end repeat
end if
return o's permutations
end generateFBNs
 
-- Generate a random factorial base number having a given number of digits.
on generateRandomFBN(numberOfDigits)
set fbnDigits to {}
repeat with maxDigit from numberOfDigits to 1 by -1
set end of fbnDigits to (random number maxDigit)
end repeat
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set fbn to fbnDigits as text
set AppleScript's text item delimiters to astid
return fbn
end generateRandomFBN
 
(* Test code *)
 
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
 
-- 1. Reproduce table of {0, 1, 2, 3} permutations
set output to {"1. Reproduce table of {0, 1, 2, 3} permutations:"}
set elements to {0, 1, 2, 3}
set listOfFBNs to generateFBNs((count elements) - 1)
repeat with fbn in listOfFBNs
copy elements to |Ω|
FBNShuffle(|Ω|, fbn)
set end of output to fbn's contents & " -> " & |Ω|
end repeat
 
-- 2. Generate and count all 11-digit permutations. No way!
set end of output to ""
set numberOfDigits to 11
set numberOfPermutations to 1
repeat with base from 2 to (numberOfDigits + 1)
set numberOfPermutations to numberOfPermutations * base
end repeat
set end of output to "2. " & numberOfDigits & "-digit factorial base numbers have " & (numberOfPermutations div 1) & " possible permutations!"
 
-- 3. Card shoe permutations with the given FBNs.
set end of output to ""
set shoe to {"A♠", "K♠", "Q♠", "J♠", "10♠", "9♠", "8♠", "7♠", "6♠", "5♠", "4♠", "3♠", "2♠", ¬
"A♥", "K♥", "Q♥", "J♥", "10♥", "9♥", "8♥", "7♥", "6♥", "5♥", "4♥", "3♥", "2♥", ¬
"A♦", "K♦", "Q♦", "J♦", "10♦", "9♦", "8♦", "7♦", "6♦", "5♦", "4♦", "3♦", "2♦", ¬
"A♣", "K♣", "Q♣", "J♣", "10♣", "9♣", "8♣", "7♣", "6♣", "5♣", "4♣", "3♣", "2♣"}
copy shoe to shoe1
copy shoe to shoe2
set fbn1 to "39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0"
set fbn2 to "51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1"
FBNShuffle(shoe1, fbn1)
FBNShuffle(shoe2, fbn2)
set end of output to "3. Shuffle " & shoe
set end of output to "With " & fbn1 & (linefeed & " -> " & shoe1)
set end of output to "With " & fbn2 & (linefeed & " -> " & shoe2)
 
-- 4. Card shoe permutation with randomly generated FBN.
set end of output to ""
set fbn3 to generateRandomFBN(51)
FBNShuffle(shoe, fbn3)
set end of output to "4. With randomly generated " & fbn3 & (linefeed & " -> " & shoe)
 
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"1. Reproduce table of {0, 1, 2, 3} permutations:
0.0.0 -> 0123
0.0.1 -> 0132
0.1.0 -> 0213
0.1.1 -> 0231
0.2.0 -> 0312
0.2.1 -> 0321
1.0.0 -> 1023
1.0.1 -> 1032
1.1.0 -> 1203
1.1.1 -> 1230
1.2.0 -> 1302
1.2.1 -> 1320
2.0.0 -> 2013
2.0.1 -> 2031
2.1.0 -> 2103
2.1.1 -> 2130
2.2.0 -> 2301
2.2.1 -> 2310
3.0.0 -> 3012
3.0.1 -> 3021
3.1.0 -> 3102
3.1.1 -> 3120
3.2.0 -> 3201
3.2.1 -> 3210
 
2. 11-digit factorial base numbers have 479001600 possible permutations!
 
3. Shuffle A♠K♠Q♠J♠10♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥10♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦10♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣10♣9♣8♣7♣6♣5♣4♣3♣2♣
With 39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
-> A♣3♣7♠4♣10♦8♦Q♠K♥2♠10♠4♦7♣J♣5♥10♥10♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
With 51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
-> 2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
4. With randomly generated 46.27.4.19.47.40.26.27.13.32.37.14.37.20.9.15.33.13.16.29.14.11.14.6.8.4.5.13.4.4.14.15.6.17.15.4.5.12.3.0.7.10.7.1.2.1.5.0.2.2.1
-> 7♣K♦10♠7♥2♣10♣J♦9♦K♥2♦8♣J♥5♣3♥4♠8♥6♣10♥4♥J♣6♥A♥2♥7♠3♠9♠6♠8♦8♠5♠4♦A♣9♥4♣Q♣2♠5♥K♣J♠A♠6♦3♣5♦Q♠A♦Q♥9♣K♠7♦3♦10♦Q♦"</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
;The Functıons:
<langsyntaxhighlight lang="fsharp">
// Factorial base numbers indexing permutations of a collection
// Nigel Galloway: December 7th., 2018
Line 70 ⟶ 221:
let fN g=if n.[g]=Ω-g then n.[g]<-0; false else n.[g]<-n.[g]+1; true
seq{yield n; while [1..Ω]|>List.exists(fun g->fN (Ω-g)) do yield n}
</syntaxhighlight>
</lang>
 
;Re-create the table:
<langsyntaxhighlight lang="fsharp">
lN [|0;0;0|] |> Seq.iter (fun n->printfn "%A -> %A" n (lN2p n [|0;1;2;3|]));;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 105 ⟶ 256:
 
;Shuffles:
<langsyntaxhighlight lang="fsharp">
let shoe==[|"A♠";"K♠";"Q♠";"J♠";"10♠";"9♠";"8♠";"7♠";"6♠";"5♠";"4♠";"3♠";"2♠";"A♥";"K♥";"Q♥";"J♥";"10♥";"9♥";"8♥";"7♥";"6♥";"5♥";"4♥";"3♥";"2♥";"A♦";"K♦";"Q♦";"J♦";"10♦";"9♦";"8♦";"7♦";"6♦";"5♦";"4♦";"3♦";"2♦";"A♣";"K♣";"Q♣";"J♣";"10♣";"9♣";"8♣";"7♣";"6♣";"5♣";"4♣";"3♣";"2♣";|]
//Random Shuffle
Line 112 ⟶ 263:
lN2p [|39;49;7;47;29;30;2;12;10;3;29;37;33;17;12;31;29;34;17;25;2;4;25;4;1;14;20;6;21;18;1;1;1;4;0;5;15;12;4;3;10;10;9;1;6;5;5;3;0;0;0|] shoe|>Array.iter (printf "%s ");printfn ""
lN2p [|51;48;16;22;3;0;19;34;29;1;36;30;12;32;12;29;30;26;14;21;8;12;1;3;10;4;7;17;6;21;8;12;15;15;13;15;7;3;12;11;9;5;5;6;6;3;4;0;3;2;1|] shoe|>Array.iter (printf "%s ");printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 122 ⟶ 273:
 
;Comparıson wıth [[http://www.rosettacode.org/wiki/Permutations#F.23 Permutations(F#)]]:
<langsyntaxhighlight lang="fsharp">
let g=[|0..10|]
lC 10 |> Seq.map(fun n->lc2p n g) |> Seq.length
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 132 ⟶ 283:
</pre>
8GB of memory is insufficient for rc's perm task
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: assocs io kernel literals math math.factorials
math.parser math.ranges prettyprint qw random sequences
splitting ;
RENAME: factoradic math.combinatorics.private => _factoradic
RENAME: rotate sequences.extras => _rotate
IN: rosetta-code.factorial-permutations
 
CONSTANT: shoe $[
qw{ A K Q J 10 9 8 7 6 5 4 3 2 } qw{ ♠ ♥ ♦ ♣ }
[ append ] cartesian-map flip concat
]
 
! Factor can already make factoradic numbers, but they always
! have a least-significant digit of 0 to remove.
: factoradic ( n -- seq )
_factoradic dup [ drop but-last ] unless-empty ;
 
! Convert "3.1.2.0" to { 3 1 2 0 }, for example.
: string>factoradic ( str -- seq )
"." split [ string>number ] map ;
 
! Rotate a subsequence.
! E.g. 0 2 { 3 1 2 0 } (rotate) -> { 2 3 1 0 }.
: (rotate) ( from to seq -- newseq )
[ 1 + ] dip [ snip ] [ subseq ] 3bi -1 _rotate glue ;
 
! Only rotate a subsequence if from does not equal to.
: rotate ( from to seq -- newseq )
2over = [ 2nip ] [ (rotate) ] if ;
 
! The pseudocode from the task description
: fpermute ( factoradic -- permutation )
dup length 1 + <iota> swap <enumerated>
[ over + rot rotate ] assoc-each ;
 
! Use a factoradic number to index permutations of a collection.
: findex ( factoradic seq -- permutation )
[ fpermute ] [ nths concat ] bi* ;
 
: .f ( seq -- ) [ "." write ] [ pprint ] interleave ; ! Print a factoradic number
: .p ( seq -- ) [ pprint ] each nl ; ! Print a permutation
 
: show-table ( -- )
"Generate table" print 24
[ factoradic 3 0 pad-head dup .f fpermute " -> " write .p ]
each-integer nl ;
 
: show-shuffles ( -- )
"Generate given task shuffles" print
"Original deck:" print shoe concat print nl
"39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0"
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1"
[ [ print ] [ string>factoradic shoe findex print nl ] bi ] bi@ ;
 
: show-random-shuffle ( -- )
"Random shuffle:" print
51 52 [ n! ] bi@ [a,b] random factoradic shoe findex print ;
 
: main ( -- ) show-table show-shuffles show-random-shuffle ;
 
MAIN: main</syntaxhighlight>
{{out}}
<pre>
Generate table
0.0.0 -> 0123
0.0.1 -> 0132
0.1.0 -> 0213
0.1.1 -> 0231
0.2.0 -> 0312
0.2.1 -> 0321
1.0.0 -> 1023
1.0.1 -> 1032
1.1.0 -> 1203
1.1.1 -> 1230
1.2.0 -> 1302
1.2.1 -> 1320
2.0.0 -> 2013
2.0.1 -> 2031
2.1.0 -> 2103
2.1.1 -> 2130
2.2.0 -> 2301
2.2.1 -> 2310
3.0.0 -> 3012
3.0.1 -> 3021
3.1.0 -> 3102
3.1.1 -> 3120
3.2.0 -> 3201
3.2.1 -> 3210
 
Generate given task shuffles
Original deck:
A♠K♠Q♠J♠10♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥10♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦10♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣10♣9♣8♣7♣6♣5♣4♣3♣2♣
 
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
A♣3♣7♠4♣10♦8♦Q♠K♥2♠10♠4♦7♣J♣5♥10♥10♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
 
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
Random shuffle:
5♠K♣K♠4♣8♥7♠Q♥J♦3♠A♦3♣8♣6♥A♥3♥A♣10♥9♠10♣5♣J♣J♠J♥2♣K♥Q♦Q♣7♣6♦7♥2♥5♥2♠10♦2♦A♠4♦8♠4♠7♦10♠6♣9♣5♦4♥8♦9♦3♦6♠K♦9♥Q♠
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 281 ⟶ 536:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 324 ⟶ 579:
</pre>
 
=={{header|Haskell}}==
 
Factoradic representation of integer numbers in canonical form (with trailing zero).
 
<syntaxhighlight lang="haskell">import Data.List (unfoldr, intercalate)
 
newtype Fact = Fact [Int]
 
-- smart constructor
fact :: [Int] -> Fact
fact = Fact . zipWith min [0..] . reverse
 
instance Show Fact where
show (Fact ds) = intercalate "." $ show <$> reverse ds
toFact :: Integer -> Fact
toFact 0 = Fact [0]
toFact n = Fact $ unfoldr f (1, n)
where
f (b, 0) = Nothing
f (b, n) = let (q, r) = n `divMod` b
in Just (fromIntegral r, (b+1, q))
 
fromFact :: Fact -> Integer
fromFact (Fact ds) = foldr f 0 $ zip [1..] ds
where
f (b, d) r = r * b + fromIntegral d</syntaxhighlight>
 
<pre>λ> toFact 2021
2.4.4.0.2.1.0
 
λ> fromFact it
2021
 
λ> fact [2,2,1,0]
2.2.1.0</pre>
 
Correspondence with permutations:
 
<syntaxhighlight lang="haskell">toPermutation :: Fact -> [Int]
toPermutation (Fact ds) = go (reverse ds) [0.. length ds - 1]
where
go [] p = p
go (d:ds) p = case splitAt (fromIntegral d) p of
(a,x:b) -> x : go ds (a++b)
(a,[]) -> a
 
permute :: [a] -> [Int] -> [a]
permute s p = case splitAt (length s - length p) s of
(s1,s2) -> s1 ++ map (s2 !!) p</syntaxhighlight>
 
<pre>λ> toPermutation (fact [4,0,2,1,0])
[4,0,3,2,1]
 
λ> permute "abcde" $ toPermutation (fact [4,0,2,1,0])
"eadcb"
 
λ> permute "abcdefgh" $ toPermutation (fact [4,0,2,1,0])
"abchdgfe"</pre>
 
Given tasks
<syntaxhighlight lang="haskell">task1 = do
putStrLn "number\tfactoradic\tpermutation"
mapM_ display [0..23]
where
display n =
let f = toFact n
p = permute "0123" (toPermutation f)
in putStrLn $ show n ++ "\t" ++ show f ++ "\t\t(" ++ p ++ ")"
 
randomFactDigits seed = zipWith mod (random seed) [1..]
where
random = iterate $ \x -> (x * 1103515245 + 12345) `mod` (2^31-1)
 
task2 = do
putStrLn "-- First example --"
let n1 = toFact 61988771037597375208735783409763169805823569176280269403732950003152
let crate1 = permute crate $ toPermutation n1
putStrLn $ "Factoradic number:\n" ++ show n1
putStrLn $ "Corresponding crate permutation:\n" ++ unwords crate1
putStrLn "\n-- Second example --"
let n2 = toFact 80576939285541005152259046665383499297948014296200417968998877609223
let crate2 = permute crate $ toPermutation n2
putStrLn $ "Factoradic number:\n" ++ show n2
putStrLn $ "Corresponding crate permutation:\n" ++ unwords crate2
putStrLn "\n-- Random example --"
let n3 = Fact $ take 52 $ randomFactDigits 42
let crate3 = permute crate $ toPermutation n3
putStrLn $ "Factoradic number:\n" ++ show n3
putStrLn $ "Decimal representation of n:\n" ++ show (fromFact n3)
putStrLn $ "Corresponding crate permutation:\n" ++ unwords crate3
where
crate = words "A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠\
\ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥\
\ A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦\
\ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣"</syntaxhighlight>
 
<pre>λ> task1
number factoradic permutation
0 0 (0123)
1 1.0 (0132)
2 1.0.0 (0213)
3 1.1.0 (0231)
4 2.0.0 (0312)
5 2.1.0 (0321)
6 1.0.0.0 (1023)
7 1.0.1.0 (1032)
8 1.1.0.0 (1203)
9 1.1.1.0 (1230)
10 1.2.0.0 (1302)
11 1.2.1.0 (1320)
12 2.0.0.0 (2013)
13 2.0.1.0 (2031)
14 2.1.0.0 (2103)
15 2.1.1.0 (2130)
16 2.2.0.0 (2301)
17 2.2.1.0 (2310)
18 3.0.0.0 (3012)
19 3.0.1.0 (3021)
20 3.1.0.0 (3102)
21 3.1.1.0 (3120)
22 3.2.0.0 (3201)
23 3.2.1.0 (3210)
 
λ> task2
-- First example --
Factoradic number:
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0.0
Corresponding crate permutation:
A♣ 3♣ 7♠ 4♣ 10♦ 8♦ Q♠ K♥ 2♠ 10♠ 4♦ 7♣ J♣ 5♥ 10♥ 10♣ K♣ 2♣ 3♥ 5♦ J♠ 6♠ Q♣ 5♠ K♠ A♦ 3♦ Q♥ 8♣ 6♦ 9♠ 8♠ 4♠ 9♥ A♠ 6♥ 5♣ 2♦ 7♥ 8♥ 9♣ 6♣ 7♦ A♥ J♦ Q♦ 9♦ 2♥ 3♠ J♥ 4♥ K♦
 
-- Second example --
Factoradic number:
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1.0
Corresponding crate permutation:
2♣ 5♣ J♥ 4♥ J♠ A♠ 5♥ A♣ 6♦ Q♠ 9♣ 3♦ Q♥ J♣ 10♥ K♣ 10♣ 5♦ 7♥ 10♦ 3♠ 8♥ 10♠ 7♠ 6♥ 5♠ K♥ 4♦ A♥ 4♣ 2♥ 9♦ Q♣ 8♣ 7♦ 6♣ 3♥ 6♠ 7♣ 2♦ J♦ 9♥ A♦ Q♦ 8♦ 4♠ K♦ K♠ 3♣ 2♠ 8♠ 9♠
 
-- Random example --
Factoradic number:
25.36.42.26.5.9.25.5.38.24.30.19.37.7.5.20.35.28.32.6.22.19.20.14.9.5.21.23.9.22.15.10.10.17.7.8.4.14.8.2.3.8.7.6.2.0.4.2.1.2.0.0
Decimal representation of n:
39898748133187068184262739663110406401085629856403860440579024763898
Corresponding crate permutation:
2♥ 3♦ 9♣ K♦ 9♠ 4♠ J♦ 8♠ 7♣ 10♦ 2♦ 5♥ 4♣ 5♠ 7♠ Q♦ 2♣ Q♣ 5♣ 3♠ 6♦ 9♦ 7♦ 7♥ Q♥ 6♠ J♣ 6♣ 10♥ 3♣ 8♦ 8♥ 6♥ 10♣ K♥ 9♥ 10♠ 8♣ 3♥ Q♠ 2♠ 4♦ 5♦ A♦ J♠ A♠ A♣ J♥ A♥ K♣ K♠ 4♥</pre>
 
=={{header|J}}==
Line 376 ⟶ 777:
0 3 2 1
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
 
public final class FactorialBaseNumbersIndexingPermutations {
 
public static void main(String[] args) {
// Part 1
List<Integer> elements = convertToListInteger("0.1.2.3");
List<Integer> factoradic = convertToListInteger("0.0.0");
for ( int i = 0; i < factorial(4); i++ ) {
List<Integer> rotated = permutation(elements, factoradic);
System.out.println(toString(factoradic, ".") + " --> " + toString(rotated, " "));
increment(factoradic);
}
System.out.println();
// Part 2
System.out.println("Generating the permutations of 11 digits:");
final int limit = factorial(11);
elements = convertToListInteger("0.1.2.3.4.5.6.7.8.9.10");
factoradic = convertToListInteger("0.0.0.0.0.0.0.0.0.0");
for ( int i = 0; i < limit; i++ ) {
List<Integer> rotated = permutation(elements, factoradic);
if ( i < 3 || i > limit - 4 ) {
System.out.println(toString(factoradic, ".") + " --> " + toString(rotated, " "));
} else if ( i == 3 ) {
System.out.println(" [ ... ] ");
}
increment(factoradic);
}
System.out.println("Number of permutations is 11! = " + limit + System.lineSeparator());
// Part 3.
List<String> codes = List.of(
"39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14"
+ ".20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0",
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4"
+ ".7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1" );
List<String> cards = List.of( "A♠", "K♠", "Q♠", "J♠", "10♠", "9♠", "8♠", "7♠", "6♠", "5♠", "4♠", "3♠", "2♠",
"A♥", "K♥", "Q♥", "J♥", "10♥", "9♥", "8♥", "7♥", "6♥", "5♥", "4♥", "3♥", "2♥",
"A♦", "K♦", "Q♦", "J♦", "10♦", "9♦", "8♦", "7♦", "6♦", "5♦", "4♦", "3♦", "2♦",
"A♣", "K♣", "Q♣", "J♣", "10♣", "9♣", "8♣", "7♣", "6♣", "5♣", "4♣", "3♣", "2♣" );
System.out.println("Original deck of cards:");
System.out.println(toString(cards, " ") + System.lineSeparator());
System.out.println("Task shuffles:");
for ( String code : codes ) {
System.out.println(code + " --> ");
factoradic = convertToListInteger(code);
System.out.println(toString(permutation(cards, factoradic), " "));
System.out.println();
}
System.out.println("Random shuffle:");
ThreadLocalRandom random = ThreadLocalRandom.current();
factoradic.clear();
for ( int i = 0; i < 52; i++ ) {
factoradic.add(random.nextInt(cards.size() - i));
}
System.out.println(toString(factoradic, ".") + " --> ");
System.out.println(toString(permutation(cards, factoradic), " "));
}
private static <T> List<T> permutation(List<T> elements, List<Integer> factoradic) {
List<T> copy = new ArrayList<T>(elements);
int m = 0;
for ( int g : factoradic ) {
Collections.rotate(copy.subList(m, m + g + 1), 1);
m += 1;
}
return copy;
}
private static void increment(List<Integer> factoradic) {
int index = factoradic.size() - 1;
while ( index >= 0 && factoradic.get(index) == factoradic.size() - index ) {
factoradic.set(index, 0);
index -= 1;
}
if ( index >= 0 ) {
factoradic.set(index, factoradic.get(index) + 1);
}
}
private static List<Integer> convertToListInteger(String text) {
List<Integer> result = new ArrayList<Integer>();
String[] numbers = text.split("\\.");
for ( String number : numbers ) {
result.add(Integer.valueOf(number));
}
return result;
}
private static int factorial(int n) {
int factorial = 1;
for ( int i = 2; i <= n; i++ ) {
factorial *= i;
}
return factorial;
}
private static <T> String toString(List<T> factoradic, String delimiter) {
return factoradic.stream().map(String::valueOf).collect(Collectors.joining(delimiter));
}
}
</syntaxhighlight>
{{ out }}
<pre>
1.0.0 --> 1 0 2 3
1.0.1 --> 1 0 3 2
1.1.0 --> 1 2 0 3
1.1.1 --> 1 2 3 0
1.2.0 --> 1 3 0 2
1.2.1 --> 1 3 2 0
2.0.0 --> 2 0 1 3
2.0.1 --> 2 0 3 1
2.1.0 --> 2 1 0 3
2.1.1 --> 2 1 3 0
2.2.0 --> 2 3 0 1
2.2.1 --> 2 3 1 0
3.0.0 --> 3 0 1 2
3.0.1 --> 3 0 2 1
3.1.0 --> 3 1 0 2
3.1.1 --> 3 1 2 0
3.2.0 --> 3 2 0 1
3.2.1 --> 3 2 1 0
 
Generating the permutations of 11 digits:
0.0.0.0.0.0.0.0.0.0 --> 0 1 2 3 4 5 6 7 8 9 10
0.0.0.0.0.0.0.0.0.1 --> 0 1 2 3 4 5 6 7 8 10 9
0.0.0.0.0.0.0.0.1.0 --> 0 1 2 3 4 5 6 7 9 8 10
[ ... ]
10.9.8.7.6.5.4.3.1.1 --> 10 9 8 7 6 5 4 3 1 2 0
10.9.8.7.6.5.4.3.2.0 --> 10 9 8 7 6 5 4 3 2 0 1
10.9.8.7.6.5.4.3.2.1 --> 10 9 8 7 6 5 4 3 2 1 0
Number of permutations is 11! = 39916800
 
Original deck of cards:
A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥ A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣
 
Task shuffles:
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0 -->
A♣ 3♣ 7♠ 4♣ 10♦ 8♦ Q♠ K♥ 2♠ 10♠ 4♦ 7♣ J♣ 5♥ 10♥ 10♣ K♣ 2♣ 3♥ 5♦ J♠ 6♠ Q♣ 5♠ K♠ A♦ 3♦ Q♥ 8♣ 6♦ 9♠ 8♠ 4♠ 9♥ A♠ 6♥ 5♣ 2♦ 7♥ 8♥ 9♣ 6♣ 7♦ A♥ J♦ Q♦ 9♦ 2♥ 3♠ J♥ 4♥ K♦
 
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1 -->
2♣ 5♣ J♥ 4♥ J♠ A♠ 5♥ A♣ 6♦ Q♠ 9♣ 3♦ Q♥ J♣ 10♥ K♣ 10♣ 5♦ 7♥ 10♦ 3♠ 8♥ 10♠ 7♠ 6♥ 5♠ K♥ 4♦ A♥ 4♣ 2♥ 9♦ Q♣ 8♣ 7♦ 6♣ 3♥ 6♠ 7♣ 2♦ J♦ 9♥ A♦ Q♦ 8♦ 4♠ K♦ K♠ 3♣ 2♠ 8♠ 9♠
 
Random shuffle:
24.45.8.6.12.29.39.31.8.0.25.27.27.5.29.32.4.31.0.1.27.22.3.13.22.13.11.17.7.8.12.5.19.18.10.2.9.12.7.2.6.9.7.6.0.2.2.1.1.0.1.0 -->
3♥ 7♣ 6♠ 8♠ K♥ 7♦ 9♣ 4♦ 4♠ A♠ 9♦ 5♦ 3♦ 7♠ Q♣ 6♣ 9♠ 5♣ K♠ J♠ 10♣ 6♦ 3♠ 4♥ K♣ 2♥ 6♥ 8♦ 10♥ 8♥ Q♦ Q♥ 2♣ 3♣ K♦ 5♠ J♦ J♣ 5♥ 2♠ A♦ 8♣ 2♦ 10♦ Q♠ J♥ 9♥ A♥ 7♥ 10♠ 4♣ A♣
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function makefactorialbased(N, makelist)
listlist = Vector{Vector{Int}}()
count = 0
Line 438 ⟶ 998:
 
factbasenums()
</langsyntaxhighlight>{{output}}<pre>
0.0.0 -> [0, 1, 2, 3]
0.0.1 -> [0, 1, 3, 2]
Line 477 ⟶ 1,037:
</pre>
 
=={{header|Perl 6Nim}}==
<syntaxhighlight lang="nim">import algorithm, math, random, sequtils, strutils, unicode
{{works with|Rakudo|2018.11}}
Using my interpretation of the task instructions as shown on the [http://rosettacode.org/wiki/Talk:Factorial_base_numbers_indexing_permutations_of_a_collection#Mojibake_and_misspellings discussion page].
 
# Representation of a factorial base number with N digits.
<lang perl6>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
type FactorialBaseNumber[N: static Positive] = array[N, int]
 
#---------------------------------------------------------------------------------------------------
multi base (Int $n is copy, 'F', $length? is copy) {
constant @fact = [\*] 1 .. *;
my $i = $length // @fact.first: * > $n, :k;
my $f;
[ @fact[^$i].reverse.map: { ($n, $f) = $n.polymod($_); $f } ]
}
 
func permutation[T](elements: openArray[T]; n: FactorialBaseNumber): seq[T] =
sub fpermute (@a is copy, *@f) { (^@f).map: { @a[$_ .. $_ + @f[$_]].=rotate(-1) }; @a }
## Return the permutation of "elements" associated to the factorial base number "n".
result = @elements
for m, g in n:
if g > 0:
result.rotateLeft(m.int..(m + g), -1)
 
#---------------------------------------------------------------------------------------------------
put "Part 1: Generate table";
put $_.&base('F', 3).join('.') ~ ' -> ' ~ [0,1,2,3].&fpermute($_.&base('F', 3)).join for ^24;
 
func incr(n: var FactorialBaseNumber): bool =
put "\nPart 2: Compare 11! to 11! " ~ '¯\_(ツ)_/¯';
## Increment a factorial base number.
# This is kind of a weird request. Since we don't actually need to _generate_
## Return false if an overflow occurred.
# the permutations, only _count_ them: compare count of 11! vs count of 11!
var base = 1
put "11! === 11! : {11! === 11!}";
var k = 1
for i in countdown(n.high, 0):
inc base
inc n[i], k
if n[i] >= base:
n[i] = 0
k = 1
else:
k = 0
result = k == 0
 
#---------------------------------------------------------------------------------------------------
put "\nPart 3: Generate the given task shuffles";
my \Ω = <A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥
A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣
>;
 
iterator fbnSeq(n: static Positive): auto =
my @books = <
## Yield the successive factorial base numbers of length "n".
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
var result: FactorialBaseNumber[n]
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
while true:
>;
yield result
if not incr(result): break
 
#---------------------------------------------------------------------------------------------------
put "Original deck:";
put Ω.join;
 
func `$`(n: FactorialBaseNumber): string {.inline.} =
put "\n$_\n" ~ Ω[(^Ω).&fpermute($_.split: '.')].join for @books;
## Return the string representation of a factorial base number.
n.join(".")
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
put "\nPart 4: Generate a random shuffle";
 
my @shoe = (+Ω … 2).map: { (^$_).pick };
# Part 1.
put @shoe.join('.');
echo "Mapping between factorial base numbers and permutations:"
put Ω[(^Ω).&fpermute(@shoe)].join;
for n in fbnSeq(3):
echo n, " → ", "0123".permutation(n).join()
 
# Part 2.
echo ""
echo "Generating the permutations of 11 digits:"
const Target = fac(11)
var count = 0
for n in fbnSeq(10):
inc count
let perm = "0123456789A".permutation(n)
if count in 1..3 or count in (Target - 2)..Target:
echo n, " → ", perm.join()
elif count == 4:
echo "[...]"
echo "Number of permutations generated: ", count
echo "Number of permutations expected: ", Target
 
# Part 3.
const
FBNS = [
"39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0",
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1"]
Cards = ["A♠", "K♠", "Q♠", "J♠", "10♠", "9♠", "8♠", "7♠", "6♠", "5♠", "4♠", "3♠", "2♠",
"A♥", "K♥", "Q♥", "J♥", "10♥", "9♥", "8♥", "7♥", "6♥", "5♥", "4♥", "3♥", "2♥",
"A♦", "K♦", "Q♦", "J♦", "10♦", "9♦", "8♦", "7♦", "6♦", "5♦", "4♦", "3♦", "2♦",
"A♣", "K♣", "Q♣", "J♣", "10♣", "9♣", "8♣", "7♣", "6♣", "5♣", "4♣", "3♣", "2♣"]
M = Cards.len - 1
 
var fbns: array[3, FactorialBaseNumber[M]]
 
# Parse the given factorial base numbers.
for i in 0..1:
for j, n in map(FBNS[i].split('.'), parseInt):
fbns[i][j] = n
 
# Generate a random factorial base number.
randomize()
for j in 0..fbns[3].high:
fbns[2][j] = rand(0..(M - j))
 
echo ""
echo "Card permutations:"
for i in 0..2:
echo "– for ", fbns[i], ':'
echo " ", Cards.permutation(fbns[i]).join(" ")</syntaxhighlight>
 
put "\nSeems to me it would be easier to just say: Ω.pick(*).join";
put Ω.pick(*).join;</lang>
{{out}}
<pre>Mapping between factorial base numbers and permutations:
<pre>Part 1: Generate table
0.0.0 → 0123
0.0.1 → 0132
0.1.0 → 0213
0.1.1 → 0231
0.2.0 → 0312
0.2.1 → 0321
1.0.0 → 1023
1.0.1 → 1032
1.1.0 → 1203
1.1.1 → 1230
1.2.0 → 1302
1.2.1 → 1320
2.0.0 → 2013
2.0.1 → 2031
2.1.0 → 2103
2.1.1 → 2130
2.2.0 → 2301
2.2.1 → 2310
3.0.0 → 3012
3.0.1 → 3021
3.1.0 → 3102
3.1.1 → 3120
3.2.0 → 3201
3.2.1 → 3210
 
Generating the permutations of 11 digits:
0.0.0.0.0.0.0.0.0.0 → 0123456789A
0.0.0.0.0.0.0.0.0.1 → 012345678A9
0.0.0.0.0.0.0.0.1.0 → 0123456798A
[...]
10.9.8.7.6.5.4.3.1.1 → A9876543120
10.9.8.7.6.5.4.3.2.0 → A9876543201
10.9.8.7.6.5.4.3.2.1 → A9876543210
Number of permutations generated: 39916800
Number of permutations expected: 39916800
 
Card permutations:
– for 39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0:
A♣ 3♣ 7♠ 4♣ 10♦ 8♦ Q♠ K♥ 2♠ 10♠ 4♦ 7♣ J♣ 5♥ 10♥ 10♣ K♣ 2♣ 3♥ 5♦ J♠ 6♠ Q♣ 5♠ K♠ A♦ 3♦ Q♥ 8♣ 6♦ 9♠ 8♠ 4♠ 9♥ A♠ 6♥ 5♣ 2♦ 7♥ 8♥ 9♣ 6♣ 7♦ A♥ J♦ Q♦ 9♦ 2♥ 3♠ J♥ 4♥ K♦
– for 51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1:
2♣ 5♣ J♥ 4♥ J♠ A♠ 5♥ A♣ 6♦ Q♠ 9♣ 3♦ Q♥ J♣ 10♥ K♣ 10♣ 5♦ 7♥ 10♦ 3♠ 8♥ 10♠ 7♠ 6♥ 5♠ K♥ 4♦ A♥ 4♣ 2♥ 9♦ Q♣ 8♣ 7♦ 6♣ 3♥ 6♠ 7♣ 2♦ J♦ 9♥ A♦ Q♦ 8♦ 4♠ K♦ K♠ 3♣ 2♠ 8♠ 9♠
– for 34.11.16.24.33.19.10.6.20.1.25.11.22.38.6.32.23.14.26.0.17.12.27.10.1.0.5.5.17.14.17.20.0.8.14.7.4.1.4.2.8.6.7.0.5.2.0.3.1.1.1:
6♦ 3♠ 10♥ A♦ 3♦ 6♥ 4♠ 8♠ 2♥ K♠ 7♦ Q♥ 9♦ 2♣ 6♠ 7♣ 4♦ 5♥ J♣ A♠ J♦ 7♥ 5♣ 9♥ J♠ Q♠ A♥ K♥ Q♣ 2♦ 9♣ 3♣ 10♠ K♦ 10♣ 3♥ J♥ 7♠ 4♥ 2♠ K♣ 5♦ 8♣ 9♠ A♣ Q♦ 5♠ 6♣ 10♦ 8♦ 4♣ 8♥</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
sub fpermute {
my($f,@a) = @_;
my @f = split /\./, $f;
for (0..$#f) {
my @b = @a[$_ .. $_+$f[$_]];
unshift @b, splice @b, $#b, 1; # rotate(-1)
@a[$_ .. $_+$f[$_]] = @b;
}
join '', @a;
}
 
sub base {
my($n) = @_;
my @digits;
push(@digits, int $n/$_) and $n = $n % $_ for <6 2 1>; # reverse <1! 2! 3!>
join '.', @digits;
}
 
say 'Generate table';
 
for (0..23) {
my $x = base($_);
say $x . ' -> ' . fpermute($x, <0 1 2 3>)
}
 
say "\nGenerate the given task shuffles";
my @omega = qw<A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥ A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣>;
 
my @books = (
'39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0',
'51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1'
);
 
say "Original deck:";
say join '', @omega;
 
say "\n$_\n" . fpermute($_,@omega) for @books;
 
say "\nGenerate a random shuffle";
say my $shoe = join '.', map { int rand($_) } reverse 0..$#omega;
say fpermute($shoe,@omega);</syntaxhighlight>
{{out}}
<pre>Generate table
0.0.0 -> 0123
0.0.1 -> 0132
Line 549 ⟶ 1,255:
3.2.1 -> 3210
 
Generate the given task shuffles
Part 2: Compare 11! to 11! ¯\_(ツ)_/¯
11! === 11! : True
 
Part 3: Generate the given task shuffles
Original deck:
A♠K♠Q♠J♠10♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥10♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦10♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣10♣9♣8♣7♣6♣5♣4♣3♣2♣
Line 562 ⟶ 1,265:
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
Part 4: Generate a random shuffle
47.933.4630.169.282.813.369.2743.2923.140.911.2715.15.1.162.2111.2223.285.3421.30.830.1914.27.1826.227.320.2524.1513.2010.1221.141.810.9.112.1.410.0.312.511.411.2.211.10.8.12.60.13.24.4.1.2.0.1.0.0
6♣7♦10♦5♠Q♠Q♥3♠3♣K♦5♣K♥7♥6♥K♠10♠9♥4♦6♠5♦7♣4♣2♥9♣10♣A♥2♦8♣A♦5♥J♣J♠3♥4♥8♠9♦A♠A♣3♦K♣Q♣6♦J♦4♠9♠10♥Q♦8♦J♥7♠8♥2♠2♣</pre>
6♣5♠5♣10♥10♦6♠K♣9♦6♦K♠2♠5♦Q♠5♥Q♦8♦J♣2♣8♣A♥K♦9♣A♦2♦9♠4♣3♥A♣7♥2♥Q♥9♥4♥J♠4♠A♠3♠8♥J♥7♠K♥3♣10♣8♠Q♣6♥7♦7♣J♦3♦4♦10♠
 
Seems to me it would be easier to just say: Ω.pick(*).join
5♦3♠8♦10♦2♥7♠7♦Q♦A♠5♣8♣Q♠4♠2♦K♦5♠Q♥7♣10♠2♠K♠J♣9♣3♣4♥3♥4♦3♦Q♣2♣4♣J♦9♠A♣J♠10♣6♣9♦6♠10♥6♥9♥J♥7♥K♥A♦8♠A♥5♥8♥K♣6♦</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function fperm(sequence fbn, omega)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer m=0
<span style="color: #008080;">function</span> <span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">fbn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">omega</span><span style="color: #0000FF;">)</span>
for i=1 to length(fbn) do
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span>
integer g = fbn[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbn</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if g>0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">g</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fbn</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
omega[m+1..m+g+1] = omega[m+g+1]&omega[m+1..m+g]
<span style="color: #008080;">if</span> <span style="color: #000000;">g</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">omega</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">omega</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">omega</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">g</span><span style="color: #0000FF;">]</span>
m += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">m</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
return omega
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">omega</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
function factorial_base_numbers(integer size, bool countOnly)
-- translation of Go
<span style="color: #008080;">function</span> <span style="color: #000000;">factorial_base_numbers</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">size</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">countOnly</span><span style="color: #0000FF;">)</span>
sequence results = {}, res = repeat(0,size)
<span style="color: #000080;font-style:italic;">-- translation of Go</span>
integer count = 0, n = 0
<span style="color: #004080;">sequence</span> <span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">size</span><span style="color: #0000FF;">)</span>
while true do
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer radix = 2, k = n
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
while k>0 do
<span style="color: #004080;">integer</span> <span style="color: #000000;">radix</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
if not countOnly
<span style="color: #008080;">while</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
and radix <= size+1 then
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">countOnly</span>
res[size-radix+2] = mod(k,radix)
<span style="color: #008080;">and</span> <span style="color: #000000;">radix</span> <span style="color: #0000FF;"><=</span> <span style="color: #000000;">size</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">size</span><span style="color: #0000FF;">-</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">)</span>
k = floor(k/radix)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
radix += 1
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">/</span><span style="color: #000000;">radix</span><span style="color: #0000FF;">)</span>
end while
<span style="color: #000000;">radix</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if radix > size+2 then exit end if
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">radix</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">size</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if not countOnly then
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
results = append(results, res)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">countOnly</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">results</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">results</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">))</span>
n += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
return iff(countOnly?count:results)
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">countOnly</span><span style="color: #0000FF;">?</span><span style="color: #000000;">count</span><span style="color: #0000FF;">:</span><span style="color: #000000;">results</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence fbns = factorial_base_numbers(3,false)
for i=1 to length(fbns) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fbns</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">factorial_base_numbers</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
printf(1,"%v -> %v\n",{fbns[i],fperm(fbns[i],{0,1,2,3})})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbns</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%v -&gt; %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">fbns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbns</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})})</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
integer count = factorial_base_numbers(10,true)
printf(1,"Permutations generated = %d\n", count)
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">factorial_base_numbers</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
printf(1," versus factorial(11) = %d\n", factorial(11))
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Permutations generated = %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" versus factorial(11) = %d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">11</span><span style="color: #0000FF;">))</span>
procedure show_cards(sequence s)
printf(1,"\n")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=1 to length(s) do
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
integer c = s[i]-1
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string sep = iff(mod(i,13)=0 or i=length(s)?"\n":" ")
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">1</span>
puts(1,"AKQJT98765432"[mod(c,13)+1]&"SHDC"[floor(c/13)+1]&sep)
<span style="color: #004080;">string</span> <span style="color: #000000;">sep</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"AKQJT98765432"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #008000;">"SHDC"</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">/</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">sep</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function rand_fbn51()
sequence fbn51 = repeat(0,51)
<span style="color: #008080;">function</span> <span style="color: #000000;">rand_fbn51</span><span style="color: #0000FF;">()</span>
for i=1 to 51 do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fbn51</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">51</span><span style="color: #0000FF;">)</span>
fbn51[i] = rand(52-i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">51</span> <span style="color: #008080;">do</span>
end for
<span style="color: #000000;">fbn51</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">52</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
return fbn51
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">fbn51</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence fbn51s = {{39,49, 7,47,29,30, 2,12,10, 3,29,37,33,17,12,31,29,
34,17,25, 2, 4,25, 4, 1,14,20, 6,21,18, 1, 1, 1, 4,
<span style="color: #004080;">sequence</span> <span style="color: #000000;">fbn51s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">39</span><span style="color: #0000FF;">,</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">47</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">37</span><span style="color: #0000FF;">,</span><span style="color: #000000;">33</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span>
0, 5,15,12, 4, 3,10,10, 9, 1, 6, 5, 5, 3, 0, 0, 0},
<span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span><span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span>
{51,48,16,22, 3, 0,19,34,29, 1,36,30,12,32,12,29,30,
<span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
26,14,21, 8,12, 1, 3,10, 4, 7,17, 6,21, 8,12,15,15,
<span style="color: #0000FF;">{</span><span style="color: #000000;">51</span><span style="color: #0000FF;">,</span><span style="color: #000000;">48</span><span style="color: #0000FF;">,</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">19</span><span style="color: #0000FF;">,</span><span style="color: #000000;">34</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">36</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">32</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">29</span><span style="color: #0000FF;">,</span><span style="color: #000000;">30</span><span style="color: #0000FF;">,</span>
13,15, 7, 3,12,11, 9, 5, 5, 6, 6, 3, 4, 0, 3, 2, 1},
<span style="color: #000000;">26</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span>
rand_fbn51()}
<span style="color: #000000;">13</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
for i=1 to length(fbn51s) do
<span style="color: #000000;">rand_fbn51</span><span style="color: #0000FF;">()}</span>
show_cards(fperm(fbn51s[i],tagset(52)))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbn51s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for</lang>
<span style="color: #000000;">show_cards</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fperm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fbn51s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">52</span><span style="color: #0000FF;">)))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 688 ⟶ 1,391:
KC 9D 4D 8D KD 6S TD AH 8C 2D 8S 3D AS
</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 900 ⟶ 1,604:
f.write("</pre>\n")
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 984 ⟶ 1,688:
5♦6♥3♣7♥10♠K♠7♠6♣9♠Q♠8♦10♣A♣5♠6♦J♠9♥9♣J♣3♠A♥4♣J♦2♣A♠7♦K♦2♦Q♣6♠4♠J♥5♣5♥K♥3♥10♦4♥9♦10♥8♣7♣4♦2♠K♣2♥8♠Q♦A♦Q♥8♥3♦
</pre>
 
=={{header|Quackery}}==
(I have set my own tasks to illustrate the use of factorial base numbers with permutations as I do not find the assigned tasks particularly illuminating IMHO.)
 
<syntaxhighlight lang="quackery"> [ 1 swap times [ i 1+ * ] ] is ! ( n --> n )
[ dup 0 = iff [ drop 2 ] done
0
[ 1+ 2dup ! / 0 = until ]
nip ] is figits ( n --> n )
[ [] unrot 1 - times
[ i 1+ ! /mod
dip join ] drop ] is factoradic ( n n --> [ )
[ 0 swap
witheach [ i 1+ ! * + ] ] is unfactoradic ( [ --> n )
[ [] unrot witheach
[ pluck
rot swap nested join
swap ]
join ] is inversion ( [ [ --> [ )
[ over size
factoradic inversion ] is nperm ( [ n --> [ )
[ 0 unrot swap witheach
[ over find
dup dip [ pluck drop ]
rot i 1+ * + swap ]
drop ] is permnum ( [ [ --> n )
say 'The 1236880662123rd permutation of' cr
say '"uncopyrightable" is "'
$ 'uncopyrightable' 1236880662123 nperm echo$
say '".' cr cr
say 'The factorial base representation of' cr
say '1236880662123 is '
1236880662123 dup figits factoradic echo
say '.' cr cr
say '"lucentbiography" is permutation' cr
say '#' $ 'lucentbiography' $ 'uncopyrightable' permnum echo
say ' of "uncopyrightable".'</syntaxhighlight>
 
'''Output:'''
<pre>The 1236880662123rd permutation of
"uncopyrightable" is "echoingabruptly".
 
The factorial number base representation of
1236880662123 is [ 14 2 8 2 5 1 4 5 5 3 0 0 1 1 ].
 
"lucentbiography" is permutation
#1134238755307 of "uncopyrightable".
 
The factorial base number [ 13 0 1 11 0 7 8 4 0 3 2 3 0 1 ]
is 1134238755307 as a decimal.</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.11}}
Using my interpretation of the task instructions as shown on the [http://rosettacode.org/wiki/Talk:Factorial_base_numbers_indexing_permutations_of_a_collection#Mojibake_and_misspellings discussion page].
 
<syntaxhighlight lang="raku" line>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
 
multi base (Int $n is copy, 'F', $length? is copy) {
constant @fact = [\*] 1 .. *;
my $i = $length // @fact.first: * > $n, :k;
my $f;
[ @fact[^$i].reverse.map: { ($n, $f) = $n.polymod($_); $f } ]
}
 
sub fpermute (@a is copy, *@f) { (^@f).map: { @a[$_ .. $_ + @f[$_]].=rotate(-1) }; @a }
 
put "Part 1: Generate table";
put $_.&base('F', 3).join('.') ~ ' -> ' ~ [0,1,2,3].&fpermute($_.&base('F', 3)).join for ^24;
 
put "\nPart 2: Compare 11! to 11! " ~ '¯\_(ツ)_/¯';
# This is kind of a weird request. Since we don't actually need to _generate_
# the permutations, only _count_ them: compare count of 11! vs count of 11!
put "11! === 11! : {11! === 11!}";
 
put "\nPart 3: Generate the given task shuffles";
my \Ω = <A♠ K♠ Q♠ J♠ 10♠ 9♠ 8♠ 7♠ 6♠ 5♠ 4♠ 3♠ 2♠ A♥ K♥ Q♥ J♥ 10♥ 9♥ 8♥ 7♥ 6♥ 5♥ 4♥ 3♥ 2♥
A♦ K♦ Q♦ J♦ 10♦ 9♦ 8♦ 7♦ 6♦ 5♦ 4♦ 3♦ 2♦ A♣ K♣ Q♣ J♣ 10♣ 9♣ 8♣ 7♣ 6♣ 5♣ 4♣ 3♣ 2♣
>;
 
my @books = <
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
>;
 
put "Original deck:";
put Ω.join;
 
put "\n$_\n" ~ Ω[(^Ω).&fpermute($_.split: '.')].join for @books;
 
put "\nPart 4: Generate a random shuffle";
my @shoe = (+Ω … 2).map: { (^$_).pick };
put @shoe.join('.');
put Ω[(^Ω).&fpermute(@shoe)].join;
 
put "\nSeems to me it would be easier to just say: Ω.pick(*).join";
put Ω.pick(*).join;</syntaxhighlight>
{{out}}
<pre>Part 1: Generate table
0.0.0 -> 0123
0.0.1 -> 0132
0.1.0 -> 0213
0.1.1 -> 0231
0.2.0 -> 0312
0.2.1 -> 0321
1.0.0 -> 1023
1.0.1 -> 1032
1.1.0 -> 1203
1.1.1 -> 1230
1.2.0 -> 1302
1.2.1 -> 1320
2.0.0 -> 2013
2.0.1 -> 2031
2.1.0 -> 2103
2.1.1 -> 2130
2.2.0 -> 2301
2.2.1 -> 2310
3.0.0 -> 3012
3.0.1 -> 3021
3.1.0 -> 3102
3.1.1 -> 3120
3.2.0 -> 3201
3.2.1 -> 3210
 
Part 2: Compare 11! to 11! ¯\_(ツ)_/¯
11! === 11! : True
 
Part 3: Generate the given task shuffles
Original deck:
A♠K♠Q♠J♠10♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥10♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦10♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣10♣9♣8♣7♣6♣5♣4♣3♣2♣
 
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
A♣3♣7♠4♣10♦8♦Q♠K♥2♠10♠4♦7♣J♣5♥10♥10♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
 
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
Part 4: Generate a random shuffle
47.9.46.16.28.8.36.27.29.1.9.27.1.16.21.22.28.34.30.8.19.27.18.22.3.25.15.20.12.14.8.9.11.1.4.0.3.5.4.2.2.10.8.1.6.1.2.4.1.2.1
6♣5♠5♣10♥10♦6♠K♣9♦6♦K♠2♠5♦Q♠5♥Q♦8♦J♣2♣8♣A♥K♦9♣A♦2♦9♠4♣3♥A♣7♥2♥Q♥9♥4♥J♠4♠A♠3♠8♥J♥7♠K♥3♣10♣8♠Q♣6♥7♦7♣J♦3♦4♦10♠
 
Seems to me it would be easier to just say: Ω.pick(*).join
5♦3♠8♦10♦2♥7♠7♦Q♦A♠5♣8♣Q♠4♠2♦K♦5♠Q♥7♣10♠2♠K♠J♣9♣3♣4♥3♥4♦3♦Q♣2♣4♣J♦9♠A♣J♠10♣6♣9♦6♠10♥6♥9♥J♥7♥K♥A♦8♠A♥5♥8♥K♣6♦</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "random" for Random
import "./math" for Int
import "./fmt" for Fmt
 
var genFactBaseNums = Fn.new { |size, countOnly|
var results = []
var count = 0
var n = 0
while (true) {
var radix = 2
var res = null
if (!countOnly) res = List.filled(size, 0)
var k = n
while (k > 0) {
var div = (k/radix).floor
var rem = k % radix
if (!countOnly) {
if (radix <= size + 1) res[size-radix+1] = rem
}
k = div
radix = radix + 1
}
if (radix > size+2) break
count = count + 1
if (!countOnly) results.add(res)
n = n + 1
}
return [results, count]
}
 
var mapToPerms = Fn.new { |factNums|
var perms = []
var psize = factNums[0].count + 1
var start = List.filled(psize, 0)
for (i in 0...psize) start[i] = i
for (fn in factNums) {
var perm = start.toList
for (m in 0...fn.count) {
var g = fn[m]
if (g != 0) {
var first = m
var last = m + g
for (i in 1..g) {
var temp = perm[first]
for (j in first+1..last) perm[j-1] = perm[j]
perm[last] = temp
}
}
}
perms.add(perm)
}
return perms
}
 
var join = Fn.new { |ints, sep| ints.map { |i| i.toString }.join(sep) }
 
var undot = Fn.new { |s| s.split(".").map { |ss| Num.fromString(ss) }.toList }
 
var rand = Random.new()
 
// Recreate the table.
var factNums = genFactBaseNums.call(3, false)[0]
var perms = mapToPerms.call(factNums)
var i = 0
for (fn in factNums) {
Fmt.print("$s -> $s", join.call(fn, "."), join.call(perms[i], ""))
i = i + 1
}
 
// Check that the number of perms generated is equal to 11! (this takes a while).
var count = genFactBaseNums.call(10, true)[1]
Fmt.print("\nPermutations generated = $,d", count)
Fmt.print("compared to 11! which = $,d", Int.factorial(11))
System.print()
 
// Generate shuffles for the 2 given 51 digit factorial base numbers.
var fbn51s = [
"39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0",
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1"
]
factNums = [undot.call(fbn51s[0]), undot.call(fbn51s[1])]
perms = mapToPerms.call(factNums)
var shoe = "A♠K♠Q♠J♠T♠9♠8♠7♠6♠5♠4♠3♠2♠A♥K♥Q♥J♥T♥9♥8♥7♥6♥5♥4♥3♥2♥A♦K♦Q♦J♦T♦9♦8♦7♦6♦5♦4♦3♦2♦A♣K♣Q♣J♣T♣9♣8♣7♣6♣5♣4♣3♣2♣".toList
var cards = List.filled(52, null)
for (i in 0..51) {
cards[i] = shoe[2*i..2*i+1].join()
if (cards[i][0] == "T") cards[i] = "10" + cards[i][1..-1]
}
i = 0
for (fbn51 in fbn51s) {
System.print(fbn51)
for (d in perms[i]) System.write(cards[d])
System.print("\n")
i = i + 1
}
 
// Create a random 51 digit factorial base number and produce a shuffle from that.
var fbn51 = List.filled(51, 0)
for (i in 0..50) fbn51[i] = rand.int(52-i)
System.print(join.call(fbn51, "."))
perms = mapToPerms.call([fbn51])
for (d in perms[0]) System.write(cards[d])
System.print()</syntaxhighlight>
 
{{out}}
<pre>
0.0.0 -> 0123
0.0.1 -> 0132
0.1.0 -> 0213
0.1.1 -> 0231
0.2.0 -> 0312
0.2.1 -> 0321
1.0.0 -> 1023
1.0.1 -> 1032
1.1.0 -> 1203
1.1.1 -> 1230
1.2.0 -> 1302
1.2.1 -> 1320
2.0.0 -> 2013
2.0.1 -> 2031
2.1.0 -> 2103
2.1.1 -> 2130
2.2.0 -> 2301
2.2.1 -> 2310
3.0.0 -> 3012
3.0.1 -> 3021
3.1.0 -> 3102
3.1.1 -> 3120
3.2.0 -> 3201
3.2.1 -> 3210
 
Permutations generated = 39,916,800
compared to 11! which = 39,916,800
 
39.49.7.47.29.30.2.12.10.3.29.37.33.17.12.31.29.34.17.25.2.4.25.4.1.14.20.6.21.18.1.1.1.4.0.5.15.12.4.3.10.10.9.1.6.5.5.3.0.0.0
A♣3♣7♠4♣10♦8♦Q♠K♥2♠10♠4♦7♣J♣5♥10♥10♣K♣2♣3♥5♦J♠6♠Q♣5♠K♠A♦3♦Q♥8♣6♦9♠8♠4♠9♥A♠6♥5♣2♦7♥8♥9♣6♣7♦A♥J♦Q♦9♦2♥3♠J♥4♥K♦
 
51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1
2♣5♣J♥4♥J♠A♠5♥A♣6♦Q♠9♣3♦Q♥J♣10♥K♣10♣5♦7♥10♦3♠8♥10♠7♠6♥5♠K♥4♦A♥4♣2♥9♦Q♣8♣7♦6♣3♥6♠7♣2♦J♦9♥A♦Q♦8♦4♠K♦K♠3♣2♠8♠9♠
 
35.12.47.3.41.40.22.9.38.8.26.15.29.23.23.0.17.19.32.5.31.10.8.26.1.18.15.6.23.21.9.13.13.1.7.11.1.1.2.5.8.6.4.1.2.3.3.2.1.2.0
5♦2♠4♣J♠9♣10♣3♥4♠8♣5♠9♦8♥3♦J♦10♦A♠4♥K♦3♣7♠2♣J♥K♥7♣Q♠6♦Q♦A♥5♣J♣7♥8♦7♦10♠9♥4♦9♠8♠3♠5♥A♣A♦6♥6♠10♥2♦K♣2♥Q♥6♣K♠Q♣
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn fpermute(omega,num){ // eg (0,1,2,3), (0,0,0)..(3,2,1)
omega=omega.copy(); // omega gonna be mutated
foreach m,g in ([0..].zip(num)){ if(g) omega.insert(m,omega.pop(m+g)) }
omega
}</langsyntaxhighlight>
 
{{out|Part 1, Generate permutation table}}
<langsyntaxhighlight lang="zkl">foreach a,b,c in (4,3,2){
println("%d.%d.%d --> %s".fmt(a,b,c, fpermute(T(0,1,2,3),T(a,b,c)).concat()));
}</langsyntaxhighlight>
{{out}}
<pre style="height:35ex">
Line 1,026 ⟶ 2,030:
 
{{out|Part 3, Generate the given task shuffles}}
<langsyntaxhighlight lang="zkl">deck:=List();
foreach s,c in ("\u2660 \u2665 \u2666 \u2663".split(),
"A K Q J 10 9 8 7 6 5 4 3 2".split()){ deck.append(c+s) }
Line 1,033 ⟶ 2,037:
"51.48.16.22.3.0.19.34.29.1.36.30.12.32.12.29.30.26.14.21.8.12.1.3.10.4.7.17.6.21.8.12.15.15.13.15.7.3.12.11.9.5.5.6.6.3.4.0.3.2.1")
.apply(fcn(s){ s.split(".").apply("toInt") });
foreach book in (books){ println(fpermute(deck,book).concat("")); }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,041 ⟶ 2,045:
 
{{out|Part 4, Generate a random shuffle}}
<langsyntaxhighlight lang="zkl">r:=[52..2,-1].pump(List,(0).random);
println(r.concat("."),"\n",fpermute(deck,r).concat(""));</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits