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

m
(Added AppleScript.)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 7 users not shown)
Line 61:
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.
 
<langsyntaxhighlight lang="applescript">-- Permutate a list according to a given factorial base number.
on FBNShuffle(|Ω|, fbn)
set astid to AppleScript's text item delimiters
Line 168:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"1. Reproduce table of {0, 1, 2, 3} permutations:
0.0.0 -> 0123
0.0.1 -> 0132
Line 206:
 
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♦"</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
;The Functıons:
<langsyntaxhighlight lang="fsharp">
// Factorial base numbers indexing permutations of a collection
// Nigel Galloway: December 7th., 2018
Line 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 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 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 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 285:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: assocs io kernel literals math math.factorials
math.parser math.ranges prettyprint qw random sequences
splitting ;
Line 345:
: main ( -- ) show-table show-shuffles show-random-shuffle ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 389:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 536:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 578:
9♥K♥K♦2♣7♥5♠K♠6♥8♥A♥3♣4♠4♦J♦5♣J♥3♠6♦7♦A♦Q♦2♥7♣10♥8♠8♣A♠10♦Q♣8♦2♠4♥6♠J♣6♣3♦10♣9♣5♦3♥4♣J♠10♠A♣Q♠Q♥K♣9♠2♦7♠5♥9♦
</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 630 ⟶ 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 692 ⟶ 998:
 
factbasenums()
</langsyntaxhighlight>{{output}}<pre>
0.0.0 -> [0, 1, 2, 3]
0.0.1 -> [0, 1, 3, 2]
Line 730 ⟶ 1,036:
J♦9♥5♦4♥A♠8♠2♠Q♠J♥2♥9♠3♠2♣A♥5♠5♥T♥9♦8♣6♠3♦4♦A♣K♦4♠6♦6♣7♠7♦K♠7♥9♣K♥5♣J♠A♦Q♣T♣8♦T♠6♥7♣3♣T♦8♥4♣Q♥J♣Q♦3♥2♦K♣
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import algorithm, math, random, sequtils, strutils, unicode
 
# Representation of a factorial base number with N digits.
type FactorialBaseNumber[N: static Positive] = array[N, int]
 
#---------------------------------------------------------------------------------------------------
 
func permutation[T](elements: openArray[T]; n: FactorialBaseNumber): seq[T] =
## 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)
 
#---------------------------------------------------------------------------------------------------
 
func incr(n: var FactorialBaseNumber): bool =
## Increment a factorial base number.
## Return false if an overflow occurred.
var base = 1
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
 
#---------------------------------------------------------------------------------------------------
 
iterator fbnSeq(n: static Positive): auto =
## Yield the successive factorial base numbers of length "n".
var result: FactorialBaseNumber[n]
while true:
yield result
if not incr(result): break
 
#---------------------------------------------------------------------------------------------------
 
func `$`(n: FactorialBaseNumber): string {.inline.} =
## Return the string representation of a factorial base number.
n.join(".")
 
#———————————————————————————————————————————————————————————————————————————————————————————————————
 
# Part 1.
echo "Mapping between factorial base numbers and permutations:"
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>
 
{{out}}
<pre>Mapping between factorial base numbers and 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
 
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}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 777 ⟶ 1,227:
say "\nGenerate a random shuffle";
say my $shoe = join '.', map { int rand($_) } reverse 0..$#omega;
say fpermute($shoe,@omega);</langsyntaxhighlight>
{{out}}
<pre>Generate table
Line 820 ⟶ 1,270:
 
=={{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 940 ⟶ 1,393:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
"""
 
Line 1,151 ⟶ 1,604:
f.write("</pre>\n")
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,236 ⟶ 1,689:
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}}==
Line 1,242 ⟶ 1,754:
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" perl6line>sub postfix:<!> (Int $n) { (flat 1, [\*] 1..*)[$n] }
 
multi base (Int $n is copy, 'F', $length? is copy) {
Line 1,282 ⟶ 1,794:
 
put "\nSeems to me it would be easier to just say: Ω.pick(*).join";
put Ω.pick(*).join;</langsyntaxhighlight>
{{out}}
<pre>Part 1: Generate table
Line 1,329 ⟶ 1,841:
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,370 ⟶ 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,377 ⟶ 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,385 ⟶ 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,482

edits