Non-transitive dice: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(47 intermediate revisions by 15 users not shown) | |||
Line 1:
{{
Let our dice select numbers on their faces with equal probability, i.e. fair dice.
Dice may have more or less than six faces. (The possibility of there being a
Line 60:
* The order of numbers on the faces of a die is not relevant. For example, three faced die described with face numbers of 1, 2, 3 or 2, 1, 3 or any other permutation are equivalent. For the purposes of the task '''show only the permutation in lowest-first sorted order i.e. 1, 2, 3''' (and remove any of its perms).
* A die can have more than one instance of the same number on its faces, e.g. <code>2, 3, 3, 4</code>
* '''Rotations''': Any rotation of non-transitive dice from an answer is also an answer. You may ''optionally'' compute and show only one of each such rotation sets, ideally the first when sorted in a natural way. If this option is used then prominently state in the output that rotations of results are also solutions.
<br><br>
Line 84 ⟶ 85:
<br>
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68"># iterates through all possible dice #
PROC iterate = (PROC(INT, []INT)VOID f)VOID:
BEGIN
INT n := 1;
[4]INT sides;
FOR i FROM 1 TO 4 DO FOR j FROM i TO 4 DO
FOR k FROM j TO 4 DO FOR l FROM k TO 4 DO
sides := (i, j, k, l);
f(n, sides);
n +:= 1
OD OD OD OD
END;
# compares 2 dice, returns (-1, 0, 1) for a (<, =, >) b #
PROC compare = ([]INT a, []INT b)INT:
BEGIN
INT result := 0;
FOR i TO UPB a DO
FOR j TO UPB b DO
result +:= SIGN(a[i] - b[j])
OD
OD;
SIGN result
END;
# compact representation of die #
PROC str = ([]INT die)STRING:
BEGIN
STRING result := " " * UPB die;
FOR i TO UPB die DO
result[i] := REPR (die[i] + ABS "0")
OD;
result
END;
# first count all possible dice #
INT ndice := 0;
iterate((INT n, []INT sides)VOID: ndice +:= 1);
# store them by index #
[ndice][4]INT dice;
iterate((INT n, []INT sides)VOID: dice[n] := sides);
# fill the results of an all-play-all tournament #
[ndice,ndice]INT results;
FOR i TO ndice DO
FOR j FROM i TO ndice DO
results[i,j] := compare(dice[i], dice[j]);
results[j,i] := results[i,j] * -1
OD
OD;
# helper function to print the intransitive sequence #
PROC print seq = ([]INT idxs)VOID:
BEGIN
FOR i TO UPB idxs DO
IF i > 1 THEN print((" < ")) FI;
print((str(dice[idxs[i]])))
OD;
print((newline))
END;
# find all (s,t,u) where s < t < u < s #
print(("3 dice:", newline));
FOR s TO ndice DO
FOR t TO ndice DO
IF results[s,t] = -1 THEN
FOR u TO ndice DO
IF results[t,u] = -1 AND results[u,s] = -1 THEN
print seq((s, t, u, s))
FI
OD
FI
OD
OD;
# find all (s,t,u,v) where s < t < u < v < s #
print(("4 dice:", newline));
FOR s TO ndice DO
FOR t TO ndice DO
IF results[s,t] = -1 THEN
FOR u TO ndice DO
IF results[t,u] = -1 THEN
FOR v TO ndice DO
IF results[u,v] = -1 AND results[v,s] = -1 THEN
print seq((s, t, u, v, s))
FI
OD
FI
OD
FI
OD
OD</syntaxhighlight>
{{out}}
<pre>3 dice:
1144 < 2224 < 1333 < 1144
1333 < 1144 < 2224 < 1333
2224 < 1333 < 1144 < 2224
4 dice:
1144 < 2224 < 2233 < 1333 < 1144
1333 < 1144 < 2224 < 2233 < 1333
2224 < 2233 < 1333 < 1144 < 2224
2233 < 1333 < 1144 < 2224 < 2233</pre>
=={{header|F_Sharp|F#}}==
===The task (4 sided die)===
<syntaxhighlight lang="fsharp">
// Non-transitive dice. Nigel Galloway: August 9th., 2020
let
let N=seq{for n in die->(n,[for g in die do if (seq{for n in n do for g in g->compare n g}|>Seq.sum<0) then yield g])}|>Map.ofSeq
let
let
n3|>Seq.iter(fun(d1,d2,d3)->printfn "%A<%A; %A<%A; %A>%A\n" d1 d2 d2 d3 d1 d3)
{{out}}
<pre>
Line 103 ⟶ 206:
[1; 3; 3; 3]<[1; 1; 4; 4]; [1; 1; 4; 4]<[2; 2; 2; 4]; [1; 3; 3; 3]>[2; 2; 2; 4]
[2; 2; 2; 4]<[1; 3; 3; 3]; [1; 3; 3; 3]<[1; 1; 4; 4]; [2; 2; 2; 4]>[1; 1; 4; 4]
Real: 00:00:00.039
[1; 1; 4; 4]<[2; 2; 2; 4]; [2; 2; 2; 4]<[2; 2; 3; 3]; [2; 2; 3; 3]<[1; 3; 3; 3]; [1; 1; 4; 4]>[1; 3; 3; 3]
Line 108 ⟶ 212:
[2; 2; 2; 4]<[2; 2; 3; 3]; [2; 2; 3; 3]<[1; 3; 3; 3]; [1; 3; 3; 3]<[1; 1; 4; 4]; [2; 2; 2; 4]>[1; 1; 4; 4]
[2; 2; 3; 3]<[1; 3; 3; 3]; [1; 3; 3; 3]<[1; 1; 4; 4]; [1; 1; 4; 4]<[2; 2; 2; 4]; [2; 2; 3; 3]>[2; 2; 2; 4]
Real: 00:00:00.152
</pre>
===Extra credit (6 sided die)===
;Sides numbered 1..6
<syntaxhighlight lang="fsharp">
// Non-transitive diceNigel Galloway: August 9th., 2020
let die=[for n0 in [1..6] do for n1 in [n0..6] do for n2 in [n1..6] do for n3 in [n2..6] do for n4 in [n3..6] do for n5 in [n4..6]->[n0;n1;n2;n3;n4;n5]]
let N=seq{for n in die->(n,[for g in die do if (seq{for n in n do for g in g->compare n g}|>Seq.sum<0) then yield g])}|>Map.ofSeq
let n3=[for d1 in die do for d2 in N.[d1] do for d3 in N.[d2] do if List.contains d1 N.[d3] then yield (d1,d2,d3)]
let d1,d2,d3=List.last n3 in printfn "Solutions found = %d\nLast solution found is %A<%A; %A<%A; %A>%A" (n3.Length) d1 d2 d2 d3 d1 d3
</syntaxhighlight>
{{out}}
<pre>
Solutions found = 121998
Last solution found is [5; 5; 5; 5; 5; 6]<[3; 3; 5; 6; 6; 6]; [3; 3; 5; 6; 6; 6]<[4; 4; 4; 6; 6; 6]; [5; 5; 5; 5; 5; 6]>[4; 4; 4; 6; 6; 6]
Real: 00:05:32.984
</pre>
;Sides numbered 1..7
<syntaxhighlight lang="fsharp">
// Non-transitive diceNigel Galloway: August 9th., 2020
let die=[for n0 in [1..7] do for n1 in [n0..7] do for n2 in [n1..7] do for n3 in [n2..7] do for n4 in [n3..7] do for n5 in [n4..7]->[n0;n1;n2;n3;n4;n5]]
let N=seq{for n in die->(n,[for g in die do if (seq{for n in n do for g in g->compare n g}|>Seq.sum<0) then yield g])}|>Map.ofSeq
let n3=[for d1 in die do for d2 in N.[d1] do for d3 in N.[d2] do if List.contains d1 N.[d3] then yield (d1,d2,d3)]
let d1,d2,d3=List.last n3 in printfn "Solutions found = %d\nLast solution found is %A<%A; %A<%A; %A>%A" (n3.Length) d1 d2 d2 d3 d1 d3
</syntaxhighlight>
{{out}}
<pre>
Solutions found = 1269189
Last solution found is [6; 6; 6; 6; 6; 7]<[4; 4; 6; 7; 7; 7]; [4; 4; 6; 7; 7; 7]<[5; 5; 5; 7; 7; 7]; [6; 6; 6; 6; 6; 7]>[5; 5; 5; 7; 7; 7]
Real: 01:21:36.912
</pre>
=={{header|Factor}}==
<
prettyprint sequences ;
Line 135 ⟶ 269:
"All ordered lists of 4 non-transitive dice with 4 sides:" print
4 4 find-non-transitive .</
{{out}}
<pre>
Line 158 ⟶ 292:
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 265 ⟶ 399:
fmt.Println(a)
}
}</
{{out}}
Line 283 ⟶ 417:
</pre>
=={{header|
<syntaxhighlight lang="haskell">{-# language LambdaCase #-}
import Data.List
import Control.Monad
newtype Dice = Dice [Int]
instance Show Dice where
show (Dice s) = "(" ++ unwords (show <$> s) ++
instance Eq Dice where
d1 == d2 = d1 `compare` d2 == EQ
instance Ord Dice where
Dice d1 `compare` Dice d2 = (add $ compare <$> d1 <*> d2) `compare` 0
where
add = sum . map (\case {LT -> -1; EQ -> 0; GT -> 1})
dices n = Dice <$> (nub $ sort <$> replicateM n [1..n])
nonTrans dice = filter (\x -> last x < head x) . go
where
go 0 = []
go 1 = sequence [dice]
go n = do
(a:as) <- go (n-1)
b <- filter (< a) dice
return (b:a:as)</syntaxhighlight>
<pre>*Main> mapM_ print $ nonTrans (dices 4) 3
[(2 2 2 4),(1 3 3 3),(1 1 4 4)]
[(1 1 4 4),(2 2 2 4),(1 3 3 3)]
[(1 3 3 3),(1 1 4 4),(2 2 2 4)]
*Main> mapM_ print $ nonTrans (dices 4) 4
[(2 2 2 4),(2 2 3 3),(1 3 3 3),(1 1 4 4)]
[(1 1 4 4),(2 2 2 4),(2 2 3 3),(1 3 3 3)]
[(2 2 3 3),(1 3 3 3),(1 1 4 4),(2 2 2 4)]
[(1 3 3 3),(1 1 4 4),(2 2 2 4),(2 2 3 3)]
*Main> length $ nonTrans (dices 4) 5
0
*Main> length $ nonTrans (dices 5) 3
2022</pre>
=={{header|J}}==
Implementation (rotations of results here are also solutions):
<syntaxhighlight lang="j">NB. unique list of all y faced dice
udice=: {{ 1+~./:~"1 (#: ,@i.)y#y }}
NB. which dice are less than which other dice?
NB. y here is a result of udice
lthan=: {{ 0>*@(+/)@,@:*@(-/)"1/~ y}}
NB. "less than loops" length x, for y sided non-transitive dice
cycles=: {{
ud=. udice y
lt=. lthan ud
extend=. [:; lt{{< y,"1 0 y-.~I.m{~{:y }}"1
r=. ; extend^:(x-1)&.> i.#ud
ud{~ ~.((i.<./)|.])"1 r #~ lt{~({:,&.>{.)|:r
}}</syntaxhighlight>
Here, <tt>extend</tt> takes a list of die sequences and appends each possibility of a new die where the new die is not less than the last die in the sequence. So, <tt>r</tt> is all such lists of length x. We discard the cases where the last element of the list is not less than the first, rotate each dice index list into canonical order, discard duplicates, and then use these indices to select the associated dice.
Task examples:
<syntaxhighlight lang="j"> 3 cycles 4
1 1 4 4
2 2 2 4
1 3 3 3
4 cycles 4
1 1 4 4
2 2 2 4
2 2 3 3
1 3 3 3</syntaxhighlight>
=={{header|Java}}==
{{trans|Kotlin}}
<syntaxhighlight lang="java">import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
private static List<List<Integer>> fourFaceCombos() {
List<List<Integer>> res = new ArrayList<>();
Set<Integer> found = new HashSet<>();
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
for (int k = 1; k <= 4; k++) {
for (int l = 1; l <= 4; l++) {
List<Integer> c = IntStream.of(i, j, k, l).sorted().boxed().collect(Collectors.toList());
int key = 64 * (c.get(0) - 1) + 16 * (c.get(1) - 1) + 4 * (c.get(2) - 1) + (c.get(3) - 1);
if (found.add(key)) {
res.add(c);
}
}
}
}
}
return res;
}
private static int cmp(List<Integer> x, List<Integer> y) {
int xw = 0;
int yw = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (x.get(i) > y.get(j)) {
xw++;
} else if (x.get(i) < y.get(j)) {
yw++;
}
}
}
return Integer.compare(xw, yw);
}
private static List<List<List<Integer>>> findIntransitive3(List<List<Integer>> cs) {
int c = cs.size();
List<List<List<Integer>>> res = new ArrayList<>();
for (int i = 0; i < c; i++) {
for (int j = 0; j < c; j++) {
if (cmp(cs.get(i), cs.get(j)) == -1) {
for (List<Integer> kl : cs) {
if (cmp(cs.get(j), kl) == -1 && cmp(kl, cs.get(i)) == -1) {
res.add(List.of(cs.get(i), cs.get(j), kl));
}
}
}
}
}
return res;
}
private static List<List<List<Integer>>> findIntransitive4(List<List<Integer>> cs) {
int c = cs.size();
List<List<List<Integer>>> res = new ArrayList<>();
for (int i = 0; i < c; i++) {
for (int j = 0; j < c; j++) {
if (cmp(cs.get(i), cs.get(j)) == -1) {
for (int k = 0; k < cs.size(); k++) {
if (cmp(cs.get(j), cs.get(k)) == -1) {
for (List<Integer> ll : cs) {
if (cmp(cs.get(k), ll) == -1 && cmp(ll, cs.get(i)) == -1) {
res.add(List.of(cs.get(i), cs.get(j), cs.get(k), ll));
}
}
}
}
}
}
}
return res;
}
public static void main(String[] args) {
List<List<Integer>> combos = fourFaceCombos();
System.out.printf("Number of eligible 4-faced dice: %d%n", combos.size());
System.out.println();
List<List<List<Integer>>> it3 = findIntransitive3(combos);
System.out.printf("%d ordered lists of 3 non-transitive dice found, namely:%n", it3.size());
for (List<List<Integer>> a : it3) {
System.out.println(a);
}
System.out.println();
List<List<List<Integer>>> it4 = findIntransitive4(combos);
System.out.printf("%d ordered lists of 4 non-transitive dice found, namely:%n", it4.size());
for (List<List<Integer>> a : it4) {
System.out.println(a);
}
}
}</syntaxhighlight>
{{out}}
<pre>Number of eligible 4-faced dice: 35
3 ordered lists of 3 non-transitive dice found, namely:
[[1, 1, 4, 4], [2, 2, 2, 4], [1, 3, 3, 3]]
[[1, 3, 3, 3], [1, 1, 4, 4], [2, 2, 2, 4]]
[[2, 2, 2, 4], [1, 3, 3, 3], [1, 1, 4, 4]]
4 ordered lists of 4 non-transitive dice found, namely:
[[1, 1, 4, 4], [2, 2, 2, 4], [2, 2, 3, 3], [1, 3, 3, 3]]
[[1, 3, 3, 3], [1, 1, 4, 4], [2, 2, 2, 4], [2, 2, 3, 3]]
[[2, 2, 2, 4], [2, 2, 3, 3], [1, 3, 3, 3], [1, 1, 4, 4]]
[[2, 2, 3, 3], [1, 3, 3, 3], [1, 1, 4, 4], [2, 2, 2, 4]]</pre>
=={{header|Julia}}==
{{trans|Python}}
<syntaxhighlight lang="julia">import Base.>, Base.<
using Memoize, Combinatorics
struct Die
name::String
faces::Vector{Int}
end
""" Compares two die returning 1, -1 or 0 for operators >, < == """
function cmpd(die1, die2)
# Numbers of times one die wins against the other for all combinations
# cmp(x, y) is `(x > y) - (y > x)` to return 1, 0, or -1 for numbers
tot = [0, 0, 0]
for d in Iterators.product(die1.faces, die2.faces)
tot[2 + (d[1] > d[2]) - (d[2] > d[1])] += 1
end
return (tot[3] > tot[1]) - (tot[1] > tot[3])
end
Base.:>(d1::Die, d2::Die) = cmpd(d1, d2) == 1
Base.:<(d1::Die, d2::Die) = cmpd(d1, d2) == -1
""" True iff ordering of die in dice is non-transitive """
isnontrans(dice) = all(x -> x[1] < x[2], zip(dice[1:end-1], dice[2:end])) && dice[1] > dice[end]
findnontrans(alldice, n=3) = [perm for perm in permutations(alldice, n) if isnontrans(perm)]
function possible_dice(sides, mx)
println("\nAll possible 1..$mx $sides-sided dice")
dice = [Die("D $(n+1)", [i for i in f]) for (n, f) in
enumerate(Iterators.product(fill(1:mx, sides)...))]
println(" Created $(length(dice)) dice")
println(" Remove duplicate with same bag of numbers on different faces")
uniquedice = collect(values(Dict(sort(d.faces) => d for d in dice)))
println(" Return $(length(uniquedice)) filtered dice")
return uniquedice
end
""" Compares two die returning their relationship of their names as a string """
function verbosecmp(die1, die2)
# Numbers of times one die wins against the other for all combinations
win1 = sum(p[1] > p[2] for p in Iterators.product(die1.faces, die2.faces))
win2 = sum(p[2] > p[1] for p in Iterators.product(die1.faces, die2.faces))
n1, n2 = die1.name, die2.name
return win1 > win2 ? "$n1 > $n2" : win1 < win2 ? "$n1 < $n2" : "$n1 = $n2"
end
""" Dice cmp function with verbose extra checks """
function verbosedicecmp(dice)
return join([[verbosecmp(x, y) for (x, y) in zip(dice[1:end-1], dice[2:end])];
[verbosecmp(dice[1], dice[end])]], ", ")
end
function testnontransitivedice(faces)
dice = possible_dice(faces, faces)
for N in (faces < 5 ? [3, 4] : [3]) # length of non-transitive group of dice searched for
nontrans = unique(map(x -> sort!(x, lt=(x, y)->x.name<y.name), findnontrans(dice, N)))
println("\n Unique sorted non_transitive length-$N combinations found: $(length(nontrans))")
for list in (faces < 5 ? nontrans : [nontrans[end]])
println(faces < 5 ? "" : " Only printing last example for brevity")
for (i, die) in enumerate(list)
println(" $die$(i < N - 1 ? "," : "]")")
end
end
if !isempty(nontrans)
println("\n More verbose comparison of last non_transitive result:")
println(" ", verbosedicecmp(nontrans[end]))
end
println("\n ====")
end
end
@time testnontransitivedice(3)
@time testnontransitivedice(4)
@time testnontransitivedice(5)
@time testnontransitivedice(6)
</syntaxhighlight>{{out}}
<pre>
All possible 1..3 3-sided dice
Created 27 dice
Remove duplicate with same bag of numbers on different faces
Return 10 filtered dice
Unique sorted non_transitive length-3 combinations found: 0
====
Unique sorted non_transitive length-4 combinations found: 0
====
0.762201 seconds (1.72 M allocations: 89.509 MiB, 1.65% gc time)
All possible 1..4 4-sided dice
Created 256 dice
Remove duplicate with same bag of numbers on different faces
Return 35 filtered dice
Unique sorted non_transitive length-3 combinations found: 1
Die("D 170", [1, 3, 3, 3]),
Die("D 215", [2, 2, 2, 4])]
Die("D 242", [1, 1, 4, 4])]
More verbose comparison of last non_transitive result:
D 170 > D 215, D 215 > D 242, D 170 < D 242
====
Unique sorted non_transitive length-4 combinations found: 1
Die("D 167", [2, 2, 3, 3]),
Die("D 170", [1, 3, 3, 3]),
Die("D 215", [2, 2, 2, 4])]
Die("D 242", [1, 1, 4, 4])]
More verbose comparison of last non_transitive result:
D 167 < D 170, D 170 > D 215, D 215 > D 242, D 167 = D 242
====
1.075354 seconds (8.15 M allocations: 1.075 GiB, 11.05% gc time)
All possible 1..5 5-sided dice
Created 3125 dice
Remove duplicate with same bag of numbers on different faces
Return 126 filtered dice
Unique sorted non_transitive length-3 combinations found: 674
Only printing last example for brevity
Die("D 2938", [2, 3, 3, 4, 5]),
Die("D 3058", [2, 2, 3, 5, 5])]
Die("D 3082", [1, 2, 4, 5, 5])]
More verbose comparison of last non_transitive result:
D 2938 > D 3058, D 3058 > D 3082, D 2938 < D 3082
====
2.422382 seconds (11.51 M allocations: 2.834 GiB, 18.87% gc time)
All possible 1..6 6-sided dice
Created 46656 dice
Remove duplicate with same bag of numbers on different faces
Return 462 filtered dice
Unique sorted non_transitive length-3 combinations found: 40666
Only printing last example for brevity
Die("D 35727", [2, 3, 3, 4, 4, 5]),
Die("D 41991", [2, 3, 3, 3, 3, 6])]
Die("D 46484", [1, 2, 2, 6, 6, 6])]
More verbose comparison of last non_transitive result:
D 35727 > D 41991, D 41991 > D 46484, D 35727 < D 46484
====
123.182432 seconds (553.51 M allocations: 392.013 GiB, 17.02% gc time
</pre>
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">fun fourFaceCombos(): List<Array<Int>> {
val res = mutableListOf<Array<Int>>()
val found = mutableSetOf<Int>()
for (i in 1..4) {
for (j in 1..4) {
for (k in 1..4) {
for (l in 1..4) {
val c = arrayOf(i, j, k, l)
c.sort()
val key = 64 * (c[0] - 1) + 16 * (c[1] - 1) + 4 * (c[2] - 1) + (c[3] - 1)
if (!found.contains(key)) {
found.add(key)
res.add(c)
}
}
}
}
}
return res
}
fun cmp(x: Array<Int>, y: Array<Int>): Int {
var xw = 0
var yw = 0
for (j in 0
}
}
}
if (xw < yw) {
return -1
}
if (xw > yw) {
return 1
}
return 0
}
fun findIntransitive3(cs: List<Array<Int>>): List<Array<Array<Int>>> {
val c = cs.size
for (i in 0 until c) {
for (j in 0 until c) {
for (k in 0 until c) {
if (cmp(cs[j], cs[k]) == -1 && cmp(cs[k], cs[i]) == -1) {
}
}
return res
}
fun findIntransitive4(cs: List<Array<Int>>): List<Array<Array<Int>>> {
val c = cs.size
val res = mutableListOf<Array<Array<Int>>>()
for (i in 0 until c) {
for (l in 0 until c) {
if (cmp(cs[k], cs[l]) == -1 && cmp(cs[l], cs[i]) == -1) {
res.add(arrayOf(cs[i], cs[j], cs[k], cs[l]))
}
}
}
}
}
}
}
return res
}
fun main() {
val combos = fourFaceCombos()
println("Number of eligible 4-faced dice: ${combos.size}")
println()
val it3 = findIntransitive3(combos)
println("${it3.size} ordered lists of 3 non-transitive dice found, namely:")
for (a in it3) {
println(a.joinToString(", ", "[", "]") { it.joinToString(", ", "[", "]") })
}
println()
val it4 = findIntransitive4(combos)
println("${it4.size} ordered lists of 4 non-transitive dice found, namely:")
for (a in it4) {
println(a.joinToString(", ", "[", "]") { it.joinToString(", ", "[", "]") })
}
}</syntaxhighlight>
{{out}}
<pre>Number of eligible 4-faced dice: 35
3 ordered lists of 3 non-transitive dice found, namely:
[[1, 1, 4, 4], [2, 2, 2, 4], [1, 3, 3, 3]]
[[1, 3, 3, 3], [1, 1, 4, 4], [2, 2, 2, 4]]
[[2, 2, 2, 4], [1, 3, 3, 3], [1, 1, 4, 4]]
4 ordered lists of 4 non-transitive dice found, namely:
[[1, 1, 4, 4], [2, 2, 2, 4], [2, 2, 3, 3], [1, 3, 3, 3]]
[[1, 3, 3, 3], [1, 1, 4, 4], [2, 2, 2, 4], [2, 2, 3, 3]]
[[2, 2, 2, 4], [2, 2, 3, 3],
[[2, 2, 3, 3], [1, 3, 3, 3],
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[DieFight]
DieFight[d1_List, d2_List] := Module[{sets},
sets = Tuples[{d1, d2}];
sets = sets[[All, 2]] - sets[[All, 1]];
Sign[Total[Sign[sets]]]
]
ds = DeleteDuplicates[Sort /@ Tuples[Range[4], 4]];
ssis = Subsets[Range[Length[ds]], {3}];
ssis //= Map[Permutations];
ssis //= Catenate;
ssis //= Select[DieFight[ds[[#[[1]]]], ds[[#[[2]]]]] == 1 &];
ssis //= Select[DieFight[ds[[#[[2]]]], ds[[#[[3]]]]] == 1 &];
ssis //= Select[DieFight[ds[[#[[1]]]], ds[[#[[3]]]]] == -1 &];
nontransitiveds = Map[ds[[#]] &, ssis, {2}];
Column[Row[{#1, "<", #2, " ; ", #2, "<", #3, " ; ", #1, ">", #3}] & @@@ nontransitiveds]
ssis = Subsets[Range[Length[ds]], {4}];
ssis //= Map[Permutations];
ssis //= Catenate;
ssis //= Select[DieFight[ds[[#[[1]]]], ds[[#[[2]]]]] == 1 &];
ssis //= Select[DieFight[ds[[#[[2]]]], ds[[#[[3]]]]] == 1 &];
ssis //= Select[DieFight[ds[[#[[3]]]], ds[[#[[4]]]]] == 1 &];
ssis //= Select[DieFight[ds[[#[[1]]]], ds[[#[[4]]]]] == -1 &];
nontransitiveds = Map[ds[[#]] &, ssis, {2}];
Column[Row[{#1, "<", #2, " ; ", #2, "<", #3, " ; ", #3, "<", #4, " ; ", #1, ">", #4}] & @@@ nontransitiveds]</syntaxhighlight>
{{out}}
<pre>{1,1,4,4}<{2,2,2,4} ; {2,2,2,4}<{1,3,3,3} ; {1,1,4,4}>{1,3,3,3}
{1,3,3,3}<{1,1,4,4} ; {1,1,4,4}<{2,2,2,4} ; {1,3,3,3}>{2,2,2,4}
{2,2,2,4}<{1,3,3,3} ; {1,3,3,3}<{1,1,4,4} ; {2,2,2,4}>{1,1,4,4}
{1,1,4,4}<{2,2,2,4} ; {2,2,2,4}<{2,2,3,3} ; {2,2,3,3}<{1,3,3,3} ; {1,1,4,4}>{1,3,3,3}
{1,3,3,3}<{1,1,4,4} ; {1,1,4,4}<{2,2,2,4} ; {2,2,2,4}<{2,2,3,3} ; {1,3,3,3}>{2,2,3,3}
{2,2,2,4}<{2,2,3,3} ; {2,2,3,3}<{1,3,3,3} ; {1,3,3,3}<{1,1,4,4} ; {2,2,2,4}>{1,1,4,4}
{2,2,3,3}<{1,3,3,3} ; {1,3,3,3}<{1,1,4,4} ; {1,1,4,4}<{2,2,2,4} ; {2,2,3,3}>{2,2,2,4}</pre>
=={{header|MiniZinc}}==
===The model===
<syntaxhighlight lang="minizinc">
%Non transitive dice. Nigel Galloway, September 14th., 2020
int: die; int: faces; set of int: values;
predicate pN(array[1..faces] of var values: n, array[1..faces] of var values: g) = sum(i in 1..faces,j in 1..faces)(if n[i]<g[j] then 1 elseif n[i]>g[j] then -1 else 0 endif)>0;
predicate pG(array[1..faces] of var values: n) = forall(g in 1..faces-1)(n[g]<=n[g+1]);
array[1..die,1..faces] of var values: g;
constraint forall(n in 1..die)(pG(g[n,1..faces]));
constraint forall(n in 1..die-1)(pN(g[n,1..faces],g[n+1,1..faces]));
constraint pN(g[die,1..faces],g[1,1..faces]);
output[join(" ",[show(g[n,1..faces])++"<"++show(g[n+1,1..faces]) | n in 1..die-1])," "++show(g[die,1..faces])++">"++show(g[1,1..faces])];
</syntaxhighlight>
===The task(4 sided die)===
;3 die values=1..4
<pre>
Compiling non_trans.mzn, additional arguments die=3; faces=4; values=1..4;
Running non_trans.mzn
[1, 1, 4, 4]<[2, 2, 2, 4] [2, 2, 2, 4]<[1, 3, 3, 3] [1, 3, 3, 3]>[1, 1, 4, 4]
----------
[2, 2, 2, 4]<[1, 3, 3, 3] [1, 3, 3, 3]<[1, 1, 4, 4] [1, 1, 4, 4]>[2, 2, 2, 4]
----------
[1, 3, 3, 3]<[1, 1, 4, 4] [1, 1, 4, 4]<[2, 2, 2, 4] [2, 2, 2, 4]>[1, 3, 3, 3]
----------
==========
%%%mzn-stat: nodes=669
%%%mzn-stat: failures=413
%%%mzn-stat: restarts=0
%%%mzn-stat: variables=746
%%%mzn-stat: intVars=60
%%%mzn-stat: boolVariables=684
%%%mzn-stat: propagators=205
%%%mzn-stat: propagations=35274
%%%mzn-stat: peakDepth=49
%%%mzn-stat: nogoods=413
%%%mzn-stat: backjumps=256
%%%mzn-stat: peakMem=0.00
%%%mzn-stat: time=0.011
%%%mzn-stat: initTime=0.004
%%%mzn-stat: solveTime=0.007
%%%mzn-stat: baseMem=0.00
%%%mzn-stat: trailMem=0.00
%%%mzn-stat: randomSeed=1599951884
Finished in 174msec
</pre>
;4 die values=1..4
<pre>
Compiling non_trans.mzn, additional arguments die=4; faces=4; values=1..4;
Running non_trans.mzn
[1, 1, 4, 4]<[2, 2, 2, 4] [2, 2, 2, 4]<[2, 2, 3, 3] [2, 2, 3, 3]<[1, 3, 3, 3] [1, 3, 3, 3]>[1, 1, 4, 4]
----------
[2, 2, 2, 4]<[2, 2, 3, 3] [2, 2, 3, 3]<[1, 3, 3, 3] [1, 3, 3, 3]<[1, 1, 4, 4] [1, 1, 4, 4]>[2, 2, 2, 4]
----------
[1, 3, 3, 3]<[1, 1, 4, 4] [1, 1, 4, 4]<[2, 2, 2, 4] [2, 2, 2, 4]<[2, 2, 3, 3] [2, 2, 3, 3]>[1, 3, 3, 3]
----------
[2, 2, 3, 3]<[1, 3, 3, 3] [1, 3, 3, 3]<[1, 1, 4, 4] [1, 1, 4, 4]<[2, 2, 2, 4] [2, 2, 2, 4]>[2, 2, 3, 3]
----------
==========
%%%mzn-stat: nodes=1573
%%%mzn-stat: failures=856
%%%mzn-stat: restarts=0
%%%mzn-stat: variables=994
%%%mzn-stat: intVars=80
%%%mzn-stat: boolVariables=912
%%%mzn-stat: propagators=273
%%%mzn-stat: propagations=85224
%%%mzn-stat: peakDepth=65
%%%mzn-stat: nogoods=856
%%%mzn-stat: backjumps=717
%%%mzn-stat: peakMem=0.00
%%%mzn-stat: time=0.022
%%%mzn-stat: initTime=0.007
%%%mzn-stat: solveTime=0.015
%%%mzn-stat: baseMem=0.00
%%%mzn-stat: trailMem=0.01
%%%mzn-stat: randomSeed=1599951808
Finished in 190msec
</pre>
===Extra Credt(6 sided die)===
;3 die values=1..6
<pre>
Compiling non_trans.mzn, additional arguments die=3; faces=6; values=1..6;
Running non_trans.mzn
[3, 3, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[2, 3, 5, 6, 6, 6] [2, 3, 5, 6, 6, 6]>[3, 3, 4, 6, 6, 6]
----------
[3, 3, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 3, 5, 6, 6, 6] [1, 3, 5, 6, 6, 6]>[3, 3, 4, 6, 6, 6]
----------
[2, 2, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 2, 5, 6, 6, 6] [1, 2, 5, 6, 6, 6]>[2, 2, 4, 6, 6, 6]
----------
[2, 2, 3, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 2, 5, 6, 6, 6] [1, 2, 5, 6, 6, 6]>[2, 2, 3, 6, 6, 6]
----------
[3, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 3, 5, 6, 6, 6] [1, 3, 5, 6, 6, 6]>[3, 4, 4, 6, 6, 6]
----------
[3, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[2, 3, 5, 6, 6, 6] [2, 3, 5, 6, 6, 6]>[3, 4, 4, 6, 6, 6]
----------
[3, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[3, 3, 5, 6, 6, 6] [3, 3, 5, 6, 6, 6]>[3, 4, 4, 6, 6, 6]
----------
[2, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 2, 5, 6, 6, 6] [1, 2, 5, 6, 6, 6]>[2, 4, 4, 6, 6, 6]
----------
[1, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 1, 5, 6, 6, 6] [1, 1, 5, 6, 6, 6]>[1, 4, 4, 6, 6, 6]
----------
[2, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[2, 2, 5, 6, 6, 6] [2, 2, 5, 6, 6, 6]>[2, 4, 4, 6, 6, 6]
----------
[2, 3, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 2, 5, 6, 6, 6] [1, 2, 5, 6, 6, 6]>[2, 3, 4, 6, 6, 6]
----------
[ 9 more solutions ]
[4, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 2, 5, 6, 6, 6] [1, 2, 5, 6, 6, 6]>[4, 4, 4, 6, 6, 6]
----------
[ 19 more solutions ]
[2, 4, 4, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[1, 3, 5, 6, 6, 6] [1, 3, 5, 6, 6, 6]>[2, 4, 4, 6, 6, 6]
----------
[ 39 more solutions ]
[1, 5, 5, 5, 5, 6]<[1, 2, 2, 6, 6, 6] [1, 2, 2, 6, 6, 6]<[3, 4, 5, 5, 5, 6] [3, 4, 5, 5, 5, 6]>[1, 5, 5, 5, 5, 6]
----------
[ 79 more solutions ]
[1, 2, 5, 6, 6, 6]<[2, 3, 3, 6, 6, 6] [2, 3, 3, 6, 6, 6]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]>[1, 2, 5, 6, 6, 6]
----------
[ 159 more solutions ]
[1, 1, 5, 5, 5, 6]<[2, 3, 3, 4, 6, 6] [2, 3, 3, 4, 6, 6]<[1, 4, 5, 5, 5, 5] [1, 4, 5, 5, 5, 5]>[1, 1, 5, 5, 5, 6]
----------
[ 319 more solutions ]
[2, 2, 4, 4, 6, 6]<[4, 4, 4, 4, 4, 6] [4, 4, 4, 4, 4, 6]<[1, 2, 5, 5, 5, 5] [1, 2, 5, 5, 5, 5]>[2, 2, 4, 4, 6, 6]
----------
[ 639 more solutions ]
[1, 2, 2, 2, 6, 6]<[2, 3, 3, 3, 3, 5] [2, 3, 3, 3, 3, 5]<[1, 1, 5, 5, 5, 5] [1, 1, 5, 5, 5, 5]>[1, 2, 2, 2, 6, 6]
----------
[ 1279 more solutions ]
[1, 1, 2, 6, 6, 6]<[2, 3, 4, 5, 5, 6] [2, 3, 4, 5, 5, 6]<[1, 4, 5, 5, 5, 5] [1, 4, 5, 5, 5, 5]>[1, 1, 2, 6, 6, 6]
----------
[ 2559 more solutions ]
[1, 1, 2, 6, 6, 6]<[3, 4, 4, 5, 5, 6] [3, 4, 4, 5, 5, 6]<[2, 5, 5, 5, 5, 5] [2, 5, 5, 5, 5, 5]>[1, 1, 2, 6, 6, 6]
----------
[ 5119 more solutions ]
[3, 4, 4, 4, 4, 6]<[1, 3, 5, 5, 5, 5] [1, 3, 5, 5, 5, 5]<[2, 2, 2, 6, 6, 6] [2, 2, 2, 6, 6, 6]>[3, 4, 4, 4, 4, 6]
----------
[ 10239 more solutions ]
[3, 3, 3, 3, 4, 5]<[1, 3, 3, 4, 4, 5] [1, 3, 3, 4, 4, 5]<[2, 2, 2, 5, 5, 6] [2, 2, 2, 5, 5, 6]>[3, 3, 3, 3, 4, 5]
----------
[ 20479 more solutions ]
[2, 2, 3, 3, 3, 6]<[1, 1, 4, 4, 4, 5] [1, 1, 4, 4, 4, 5]<[1, 1, 1, 5, 5, 5] [1, 1, 1, 5, 5, 5]>[2, 2, 3, 3, 3, 6]
----------
[ 40959 more solutions ]
[1, 1, 3, 3, 4, 4]<[1, 1, 1, 4, 6, 6] [1, 1, 1, 4, 6, 6]<[1, 2, 2, 3, 3, 6] [1, 2, 2, 3, 3, 6]>[1, 1, 3, 3, 4, 4]
----------
[ 40076 more solutions ]
[2, 2, 2, 4, 5, 5]<[3, 3, 3, 3, 3, 4] [3, 3, 3, 3, 3, 4]<[2, 3, 3, 3, 4, 4] [2, 3, 3, 3, 4, 4]>[2, 2, 2, 4, 5, 5]
----------
==========
%%%mzn-stat: nodes=203916
%%%mzn-stat: failures=185337
%%%mzn-stat: restarts=0
%%%mzn-stat: variables=1658
%%%mzn-stat: intVars=126
%%%mzn-stat: boolVariables=1530
%%%mzn-stat: propagators=451
%%%mzn-stat: propagations=8595895
%%%mzn-stat: peakDepth=109
%%%mzn-stat: nogoods=185337
%%%mzn-stat: backjumps=18579
%%%mzn-stat: peakMem=0.00
%%%mzn-stat: time=11.444
%%%mzn-stat: initTime=0.010
%%%mzn-stat: solveTime=11.434
%%%mzn-stat: baseMem=0.00
%%%mzn-stat: trailMem=0.01
%%%mzn-stat: randomSeed=1599951938
Finished in 12s 600msec
</pre>
;3 die values=1..7
<pre>
Compiling non_trans.mzn, additional arguments die=3; faces=6; values=1..7;
Running non_trans.mzn
[4, 4, 6, 7, 7, 7]<[4, 5, 5, 7, 7, 7] [4, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[4, 4, 6, 7, 7, 7]
----------
[3, 4, 6, 7, 7, 7]<[4, 5, 5, 7, 7, 7] [4, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[3, 4, 6, 7, 7, 7]
----------
[3, 3, 6, 7, 7, 7]<[3, 5, 5, 7, 7, 7] [3, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[3, 3, 6, 7, 7, 7]
----------
[3, 3, 6, 7, 7, 7]<[3, 4, 5, 7, 7, 7] [3, 4, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[3, 3, 6, 7, 7, 7]
----------
[3, 3, 6, 7, 7, 7]<[3, 4, 4, 7, 7, 7] [3, 4, 4, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[3, 3, 6, 7, 7, 7]
----------
[2, 3, 6, 7, 7, 7]<[3, 5, 5, 7, 7, 7] [3, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 3, 6, 7, 7, 7]
----------
[2, 4, 6, 7, 7, 7]<[4, 5, 5, 7, 7, 7] [4, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 4, 6, 7, 7, 7]
----------
[2, 2, 6, 7, 7, 7]<[2, 5, 5, 7, 7, 7] [2, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 2, 6, 7, 7, 7]
----------
[2, 3, 6, 7, 7, 7]<[3, 4, 5, 7, 7, 7] [3, 4, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 3, 6, 7, 7, 7]
----------
[2, 2, 6, 7, 7, 7]<[2, 4, 5, 7, 7, 7] [2, 4, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 2, 6, 7, 7, 7]
----------
[2, 3, 6, 7, 7, 7]<[3, 4, 4, 7, 7, 7] [3, 4, 4, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 3, 6, 7, 7, 7]
----------
[ 9 more solutions ]
[1, 2, 6, 7, 7, 7]<[2, 3, 5, 7, 7, 7] [2, 3, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[1, 2, 6, 7, 7, 7]
----------
[ 19 more solutions ]
[1, 1, 6, 7, 7, 7]<[2, 5, 5, 7, 7, 7] [2, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[1, 1, 6, 7, 7, 7]
----------
[ 39 more solutions ]
[2, 2, 6, 7, 7, 7]<[4, 5, 5, 7, 7, 7] [4, 5, 5, 7, 7, 7]<[6, 6, 6, 6, 6, 7] [6, 6, 6, 6, 6, 7]>[2, 2, 6, 7, 7, 7]
----------
[ 79 more solutions ]
[5, 5, 5, 6, 7, 7]<[1, 6, 6, 6, 6, 7] [1, 6, 6, 6, 6, 7]<[1, 2, 6, 6, 7, 7] [1, 2, 6, 6, 7, 7]>[5, 5, 5, 6, 7, 7]
----------
[ 159 more solutions ]
[1, 1, 6, 6, 6, 7]<[3, 3, 3, 4, 7, 7] [3, 3, 3, 4, 7, 7]<[1, 4, 6, 6, 6, 6] [1, 4, 6, 6, 6, 6]>[1, 1, 6, 6, 6, 7]
----------
[ 319 more solutions ]
[3, 3, 4, 6, 6, 7]<[4, 4, 5, 5, 5, 7] [4, 4, 5, 5, 5, 7]<[1, 2, 6, 6, 6, 7] [1, 2, 6, 6, 6, 7]>[3, 3, 4, 6, 6, 7]
----------
[ 639 more solutions ]
[2, 2, 2, 6, 6, 7]<[5, 5, 5, 5, 5, 7] [5, 5, 5, 5, 5, 7]<[1, 2, 6, 6, 6, 6] [1, 2, 6, 6, 6, 6]>[2, 2, 2, 6, 6, 7]
----------
[ 1279 more solutions ]
[1, 2, 2, 6, 7, 7]<[4, 5, 5, 5, 5, 7] [4, 5, 5, 5, 5, 7]<[1, 1, 6, 6, 6, 6] [1, 1, 6, 6, 6, 6]>[1, 2, 2, 6, 7, 7]
----------
[ 2559 more solutions ]
[3, 3, 3, 6, 7, 7]<[3, 4, 5, 5, 7, 7] [3, 4, 5, 5, 7, 7]<[2, 6, 6, 6, 6, 6] [2, 6, 6, 6, 6, 6]>[3, 3, 3, 6, 7, 7]
----------
[ 5119 more solutions ]
[4, 4, 4, 5, 7, 7]<[5, 5, 5, 5, 5, 7] [5, 5, 5, 5, 5, 7]<[3, 4, 6, 6, 6, 6] [3, 4, 6, 6, 6, 6]>[4, 4, 4, 5, 7, 7]
----------
[ 10239 more solutions ]
[3, 4, 5, 5, 7, 7]<[5, 5, 5, 5, 5, 6] [5, 5, 5, 5, 5, 6]<[2, 2, 6, 6, 6, 6] [2, 2, 6, 6, 6, 6]>[3, 4, 5, 5, 7, 7]
----------
[ 20479 more solutions ]
[1, 3, 3, 3, 7, 7]<[2, 4, 4, 5, 5, 6] [2, 4, 4, 5, 5, 6]<[1, 2, 4, 6, 6, 6] [1, 2, 4, 6, 6, 6]>[1, 3, 3, 3, 7, 7]
----------
[ 40959 more solutions ]
[2, 3, 4, 4, 7, 7]<[3, 4, 4, 5, 5, 5] [3, 4, 4, 5, 5, 5]<[1, 1, 4, 5, 6, 7] [1, 1, 4, 5, 6, 7]>[2, 3, 4, 4, 7, 7]
----------
[ 81919 more solutions ]
[2, 2, 2, 2, 7, 7]<[1, 3, 3, 3, 3, 5] [1, 3, 3, 3, 3, 5]<[1, 2, 2, 4, 6, 6] [1, 2, 2, 4, 6, 6]>[2, 2, 2, 2, 7, 7]
----------
[ 163839 more solutions ]
[1, 1, 2, 7, 7, 7]<[2, 3, 4, 5, 5, 7] [2, 3, 4, 5, 5, 7]<[2, 3, 5, 5, 6, 6] [2, 3, 5, 5, 6, 6]>[1, 1, 2, 7, 7, 7]
----------
[ 327679 more solutions ]
[1, 1, 4, 6, 7, 7]<[2, 2, 3, 5, 7, 7] [2, 2, 3, 5, 7, 7]<[3, 4, 4, 4, 5, 7] [3, 4, 4, 4, 5, 7]>[1, 1, 4, 6, 7, 7]
----------
[ 613827 more solutions ]
[3, 3, 3, 4, 4, 5]<[2, 2, 3, 5, 6, 6] [2, 2, 3, 5, 6, 6]<[2, 2, 2, 5, 7, 7] [2, 2, 2, 5, 7, 7]>[3, 3, 3, 4, 4, 5]
----------
==========
%%%mzn-stat: nodes=1659193
%%%mzn-stat: failures=1573389
%%%mzn-stat: restarts=0
%%%mzn-stat: variables=1694
%%%mzn-stat: intVars=126
%%%mzn-stat: boolVariables=1566
%%%mzn-stat: propagators=451
%%%mzn-stat: propagations=54691502
%%%mzn-stat: peakDepth=109
%%%mzn-stat: nogoods=1573389
%%%mzn-stat: backjumps=85804
%%%mzn-stat: peakMem=0.00
%%%mzn-stat: time=128.215
%%%mzn-stat: initTime=0.010
%%%mzn-stat: solveTime=128.205
%%%mzn-stat: baseMem=0.00
%%%mzn-stat: trailMem=0.01
%%%mzn-stat: randomSeed=1599952012
Finished in 2m 9s
</pre>
=={{header|Nim}}==
{{trans|Python}}
{{libheader|itertools}}
<syntaxhighlight lang="nim">
import std/[algorithm, sequtils, sets, strformat]
import itertools
type Die = object
name: string
faces: seq[int]
####################################################################################################
# Die functions.
func `$`(die: Die): string =
## Return the string representation of a Die.
&"({die.name}: {($die.faces)[1..^1]})"
func cmp(die1, die2: Die): int =
## Compare two dice returning 1, -1 or 0 for operators >, < ==.
var tot: array[3, int]
for d in product(die1.faces, die2.faces):
inc tot[1 + ord(d[1] < d[0]) - ord(d[0] < d[1])]
result = ord(tot[0] < tot[2]) - ord(tot[2] < tot[0])
func verboseCmp(die1, die2: Die): string =
## Compare two dice returning a string.
var win1, win2 = 0
for (d1, d2) in product(die1.faces, die2.faces):
inc win1, ord(d1 > d2)
inc win2, ord(d2 > d1)
result = if win1 > win2: &"{die1.name} > {die2.name}"
elif win1 < win2: &"{die1.name} < {die2.name}"
else: &"{die1.name} = {die2.name}"
func `>`(die1, die2: Die): bool = cmp(die1, die2) > 0
func `<=`(die1, die2: Die): bool = cmp(die1, die2) <= 0
####################################################################################################
# Added a permutation iterator as that of "itertools" doesn't allow to specify the length.
iterator permutations[T](values: openArray[T]; r: int): seq[T] {.closure} =
## Yield permutations of length "r" with elements taken from "values".
let n = values.len
if r > n: return
var indices = toSeq(0..<n)
var cycles = toSeq(countdown(n, n - r + 1))
var perm = values[0..<r]
yield perm
if n == 0: return
while true:
var exit = true
for i in countdown(r - 1, 0):
dec cycles[i]
if cycles[i] == 0:
discard indices.rotateLeft(i..indices.high, 1)
cycles[i] = n - i
else:
let j = cycles[i]
swap indices[i], indices[^j]
for iperm, ivalues in indices[0..<r]:
perm[iperm] = values[ivalues]
yield perm
exit = false
break
if exit: return
####################################################################################################
# Dice functions.
func isNonTrans(dice: openArray[Die]): bool =
## Return true if ordering of die in dice is non-transitive.
for i in 1..dice.high:
if dice[i] <= dice[i-1]: return false
result = dice[0] > dice[^1]
func findNonTrans(allDice: openArray[Die]; n = 3): seq[seq[Die]] =
## Return the list of non-transitive dice.
for perm in permutations(allDice, n):
if perm.isNontrans:
result.add perm
proc possibleDice(sides, maxval: Positive): seq[Die] =
## Return the list of possible dice with given number of sides and maximum value.
echo &"All possible 1..{maxval} {sides}-sided dice."
var dice: seq[Die]
var n = 1
for faces in product(toSeq(1..maxval), repeat = sides):
dice.add Die(name: &"D{n}", faces: faces)
inc n
echo &" Created {dice.len} dice."
echo " Remove duplicate with same bag of numbers on different faces."
var found: HashSet[seq[int]]
for d in dice:
let count = sorted(d.faces)
if count notin found:
found.incl count
result.add d
echo &" Return {result.len} filtered dice."
func verboseDiceCmp(dice: openArray[Die]): string =
## Return the verbose comparison of dice.
for i in 1..dice.high:
result.add verboseCmp(dice[i-1], dice[i]) & ", "
result.add verboseCmp(dice[0], dice[^1])
#———————————————————————————————————————————————————————————————————————————————————————————————————
when isMainModule:
let dice = possibleDice(sides = 4, maxval = 4)
for n in [3, 4]:
let nonTrans = dice.findNonTrans(n)
echo &"\n Non-transitive length-{n} combinations found: {nonTrans.len}."
for list in nonTrans:
echo ""
for i, die in list:
echo " ", if i == 0: '[' else: ' ', die, if i == list.high: "]" else: ","
if nonTrans.len != 0:
echo &"\n More verbose comparison of last non-transitive result:"
echo " ", verboseDiceCmp(nonTrans[^1])
echo "\n ===="</syntaxhighlight>
{{out}}
<pre>All possible 1..4 4-sided dice.
Created 256 dice.
Remove duplicate with same bag of numbers on different faces.
Return 35 filtered dice.
Non-transitive length-3 combinations found: 3.
[(D16: [1, 1, 4, 4]),
(D88: [2, 2, 2, 4]),
(D43: [1, 3, 3, 3])]
[(D43: [1, 3, 3, 3]),
(D16: [1, 1, 4, 4]),
(D88: [2, 2, 2, 4])]
[(D88: [2, 2, 2, 4]),
(D43: [1, 3, 3, 3]),
(D16: [1, 1, 4, 4])]
More verbose comparison of last non-transitive result:
D88 < D43, D43 < D16, D88 > D16
====
Non-transitive length-4 combinations found: 4.
[(D16: [1, 1, 4, 4]),
(D88: [2, 2, 2, 4]),
(D91: [2, 2, 3, 3]),
(D43: [1, 3, 3, 3])]
[(D43: [1, 3, 3, 3]),
(D16: [1, 1, 4, 4]),
(D88: [2, 2, 2, 4]),
(D91: [2, 2, 3, 3])]
[(D88: [2, 2, 2, 4]),
(D91: [2, 2, 3, 3]),
(D43: [1, 3, 3, 3]),
(D16: [1, 1, 4, 4])]
[(D91: [2, 2, 3, 3]),
(D43: [1, 3, 3, 3]),
(D16: [1, 1, 4, 4]),
(D88: [2, 2, 2, 4])]
More verbose comparison of last non-transitive result:
D91 < D43, D43 < D16, D16 < D88, D91 > D88
====</pre>
=={{header|Perl}}==
{{trans|Go}}
<syntaxhighlight lang="perl">use strict;
use warnings;
sub fourFaceCombs {
my %found = ();
my @res = ();
for (my $i = 1; $i <= 4; $i++) {
for (my $j = 1; $j <= 4; $j++) {
for (my $k = 1; $k <= 4; $k++) {
for (my $l = 1; $l <= 4; $l++) {
my @c = sort ($i, $j, $k, $l);
my $key = 0;
for my $p (@c) {
$key = 10 * $key + $p;
}
if (not exists $found{$key}) {
$found{$key} = 1;
push @res, \@c;
}
}
}
}
}
return @res;
}
sub compare {
my $xref = shift;
my $yref = shift;
my @x = @$xref;
my $xw = 0;
my @y = @$yref;
my $yw = 0;
for my $i (@x) {
for my $j (@y) {
if ($i < $j) {
$yw++;
}
if ($j < $i) {
$xw++;
}
}
}
if ($xw < $yw) {
return -1;
}
if ($yw < $xw) {
return 1;
}
return 0;
}
sub findIntransitive3 {
my $dice_ref = shift;
my @dice = @$dice_ref;
my $len = scalar @dice;
my @res = ();
for (my $i = 0; $i < $len; $i++) {
for (my $j = 0; $j < $len; $j++) {
my $first = compare($dice[$i], $dice[$j]);
if ($first == 1) {
for (my $k = 0; $k < $len; $k++) {
my $second = compare($dice[$j], $dice[$k]);
if ($second == 1) {
my $third = compare($dice[$k], $dice[$i]);
if ($third == 1) {
my $d1r = $dice[$i];
my $d2r = $dice[$j];
my $d3r = $dice[$k];
my @itd = ($d1r, $d2r, $d3r);
push @res, \@itd;
}
}
}
}
}
}
return @res;
}
sub findIntransitive4 {
my $dice_ref = shift;
my @dice = @$dice_ref;
my $len = scalar @dice;
my @res = ();
for (my $i = 0; $i < $len; $i++) {
for (my $j = 0; $j < $len; $j++) {
for (my $k = 0; $k < $len; $k++) {
for (my $l = 0; $l < $len; $l++) {
my $first = compare($dice[$i], $dice[$j]);
if ($first == 1) {
my $second = compare($dice[$j], $dice[$k]);
if ($second == 1) {
my $third = compare($dice[$k], $dice[$l]);
if ($third == 1) {
my $fourth = compare($dice[$l], $dice[$i]);
if ($fourth == 1) {
my $d1r = $dice[$i];
my $d2r = $dice[$j];
my $d3r = $dice[$k];
my $d4r = $dice[$l];
my @itd = ($d1r, $d2r, $d3r, $d4r);
push @res, \@itd;
}
}
}
}
}
}
}
}
return @res;
}
sub main {
my @dice = fourFaceCombs();
my $len = scalar @dice;
print "Number of eligible 4-faced dice: $len\n\n";
my @it3 = findIntransitive3(\@dice);
my $count3 = scalar @it3;
print "$count3 ordered lists of 3 non-transitive dice found, namely:\n";
for my $itref (@it3) {
print "[ ";
for my $r (@$itref) {
print "[@$r] ";
}
print "]\n";
}
print "\n";
my @it4 = findIntransitive4(\@dice);
my $count = scalar @it4;
print "$count ordered lists of 4 non-transitive dice found, namely:\n";
for my $itref (@it4) {
print "[ ";
for my $r (@$itref) {
print "[@$r] ";
}
print "]\n";
}
}
main();</syntaxhighlight>
{{out}}
<pre>Number of eligible 4-faced dice: 35
3 ordered lists of 3 non-transitive dice found, namely:
[ [1 1 4 4] [1 3 3 3] [2 2 2 4] ]
[ [1 3 3 3] [2 2 2 4] [1 1 4 4] ]
[ [2 2 2 4] [1 1 4 4] [1 3 3 3] ]
4 ordered lists of 4 non-transitive dice found, namely:
[ [1 1 4 4] [1 3 3 3] [2 2 3 3] [2 2 2 4] ]
[ [1 3 3 3] [2 2 3 3] [2 2 2 4] [1 1 4 4] ]
[ [2 2 2 4] [1 1 4 4] [1 3 3 3] [2 2 3 3] ]
[ [2 2 3 3] [2 2 2 4] [1 1 4 4] [1 3 3 3] ]</pre>
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (added sq_cmp() builtin that returns nested -1/0/+1 compare() results, just like the existing sq_eq() does for equal().)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- max number of a die side (later set to 6)</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- min number of a die side (later set to 0)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">sides</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- construct all non-descending permutes of mn..mx,
-- ie/eg {1,1,1,1}..{4,4,4,4} with (say), amongst
-- others, {1,1,2,4} but not {1,1,4,2}.
--</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">die</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sides</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (main work area)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">)}</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #000080;font-style:italic;">-- find rightmost incrementable side
-- ie/eg {1,2,4,4} -> set rdx to 2 (if 1-based indexing)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_lt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</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>
<span style="color: #000080;font-style:italic;">-- set that and all later to that incremented
-- ie/eg {1,2,4,4} -> {1,3,3,3}</span>
<span style="color: #000000;">die</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">..$]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">die</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">1</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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;">"There are %d possible %d..%d %d-sided dice\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sides</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Dnn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">die</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- reconstruct the python die numbering (string)
-- essentially just treat it as a base-N number.</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (sides)</span>
<span style="color: #000000;">N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">-- (base)</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- (result)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">die</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">mn</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"D%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">cmpd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">die1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">die2</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- compares two die returning -1, 0, or +1 for <, =, ></span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<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;">die1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">die2</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sign</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">low_rotation</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">),</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">..$]&</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rotations</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">sk</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sk1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span> <span style="color: #000080;font-style:italic;">-- (indexes to dice)</span>
<span style="color: #000000;">cache</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">sk</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">sk1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cmpd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">],</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">valid</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #000080;font-style:italic;">-- force k+1 to be incremented next:</span>
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</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: #000000;">l</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">valid</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">sk</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">sk1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">[$]</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cmpd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">],</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">])</span>
<span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sk</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sk1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">low_rotation</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- find rightmost incrementable die index
-- ie/eg if l is 35 and set is {1,2,35,35}
-- -> set rdx to 2 (if 1-based indexing)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_lt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</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>
<span style="color: #000080;font-style:italic;">-- increment that and reset all later
-- ie/eg {1,2,35,35} -> {1,3,1,1}</span>
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</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;">1</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"working... (%d/%d)\r"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">l</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">progress</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">rotations</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">rotations</span> <span style="color: #0000FF;">-=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">verbose_cmp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">die1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">die2</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- compares two die returning their relationship of their names as a string</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cmpd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">die2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">op</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"<"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"="</span><span style="color: #0000FF;">,</span><span style="color: #008000;">">"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">c</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">n1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Dnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">n2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">Dnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">die2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s %s %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">op</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n2</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">verbose_dice_cmp</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">d1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d2</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</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;">set</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">d1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]]</span>
<span style="color: #000000;">d2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]]</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">verbose_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">d1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]]</span>
<span style="color: #000000;">d2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">set</span><span style="color: #0000FF;">[$]]</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">verbose_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d2</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">non_trans</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">non_trans</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">omissions</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">N</span> <span style="color: #008080;">then</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 Non_transitive length-%d combinations found: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">+</span><span style="color: #000000;">rotations</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<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;">l</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">ni</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">non_trans</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><</span><span style="color: #000000;">5</span> <span style="color: #008080;">then</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>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</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;">ni</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dice</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</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;">" %s:%v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Dnn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">),</span><span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">omissions</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">omissions</span> <span style="color: #008080;">then</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;">" (%d omitted)\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">omissions</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rotations</span> <span style="color: #008080;">then</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;">" (%d rotations omitted)\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rotations</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">last</span> <span style="color: #008080;">then</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>
<span style="color: #008080;">if</span> <span style="color: #000000;">mx</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">6</span> <span style="color: #008080;">and</span> <span style="color: #000000;">mn</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</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;">" More verbose comparison of last result:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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;">" %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">verbose_dice_cmp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">non_trans</span><span style="color: #0000FF;">[</span><span style="color: #000000;">last</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;">"\n ====\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</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>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">4</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">N</span><span style="color: #0000FF;">),</span><span style="color: #000000;">N</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- From the numberphile video (Efron's dice):</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--show_dice(dice,find_non_trans(dice,4),4)
-- ok, dunno about you but I'm not waiting for power(924,4) permutes...
-- limit to the ones discussed, plus another 4 random ones
-- (hopefully it'll just pick out the right ones...)</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;">"\nEfron's dice\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</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;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</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;">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: #000080;font-style:italic;">-- (rand)</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;">5</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: #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><span style="color: #000000;">4</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: #000080;font-style:italic;">-- (rand)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</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: #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;">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;">6</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- (rand)</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</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;">6</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;">6</span><span style="color: #0000FF;">}}</span> <span style="color: #000080;font-style:italic;">-- (rand)</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</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: #000080;font-style:italic;">-- and from wp:</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">9</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</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;">"\nFrom wp\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</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;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</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;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</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;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}}</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</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: #000080;font-style:italic;">-- Miwin's dice</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;">"Miwin's dice\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</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;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</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;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">},</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><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">}}</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</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: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 513 ⟶ 1,786:
D16:{1,1,4,4}
D88:{2,2,2,4}
D43:{1,3,3,3}
(2 rotations omitted)
More verbose comparison of last result:
D16
====
Line 526 ⟶ 1,799:
D16:{1,1,4,4}
D88:{2,2,2,4}
D91:{2,2,3,3}
D43:{1,3,3,3}
(3 rotations omitted)
More verbose comparison of last result:
D16
====
Line 566 ⟶ 1,839:
D9945:{1,2,5,6,7,9}
D74825:{2,3,4,6,7,8}
D15705:{1,3,4,5,8,9}
(2 rotations omitted)
D9945
====
"0.3s"
</pre>
=== Stretch: length-3 combinations of
I have also estimated that power(3003,3) (ie 1..9 on the sides) would probably take around 4 hours (assuming such attempts didn't run out of memory).
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #000000;">3</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;">"%s\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">mx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span>
<span style="color: #000000;">mn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #000000;">dice</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">possible_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">show_dice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">find_non_trans</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dice</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #000000;">3</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;">"%s\n\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 587 ⟶ 1,874:
D58:{1,1,1,2,4,4}
D262:{1,1,2,2,2,4}
D88:{1,1,1,3,3,4}
D58:{1,1,1,2,4,4}
D1556:{1,2,2,2,2,2}
D87:{1,1,1,3,3,3}
D58:{1,1,1,2,4,4}
D1556:{1,2,2,2,2,2}
D88:{1,1,1,3,3,4}
D58:{1,1,1,2,4,4}
D1557:{1,2,2,2,2,3}
D88:{1,1,1,3,3,4}
(40662 omitted)
(81332 rotations omitted)
More verbose comparison of last result:
D58
====
"30.9s"
There are 924 possible 0..6 6-sided dice
Non_transitive length-3 combinations found: 1269189
D74:{0,0,0,1,3,3}
D403:{0,0,1,1,1,3}
D116:{0,0,0,2,2,3}
D74:{0,0,0,1,3,3}
D2802:{0,1,1,1,1,1}
D115:{0,0,0,2,2,2}
D74:{0,0,0,1,3,3}
D2802:{0,1,1,1,1,1}
D116:{0,0,0,2,2,3}
D74:{0,0,0,1,3,3}
D2803:{0,1,1,1,1,2}
D116:{0,0,0,2,2,3}
(423059 omitted)
(846126 rotations omitted)
D74 < D2803, D2803 < D116, D74 > D116
====
"3 minutes and 60s"
</pre>
=={{header|Python}}==
Trivial rotations of the same loop are not shown.
<syntaxhighlight lang="python">from itertools import combinations_with_replacement as cmbr
from time import time
def dice_gen(n, faces, m):
dice = list(cmbr(faces, n))
succ = [set(j for j, b in enumerate(dice)
if sum((x>y) - (x<y) for x in a for y in b) > 0)
for a in dice]
def loops(seq):
s = succ[seq[-1]]
if len(seq) == m:
if seq[0] in s: yield seq
return
for d in (x for x in s if x > seq[0] and not x in seq):
yield from loops(seq + (d,))
yield from (tuple(''.join(dice[s]) for s in x)
for i, v in enumerate(succ)
for x in loops((i,)))
t = time()
for n, faces, loop_len in [(4, '1234', 3), (4, '1234', 4), (6, '123456', 3), (6, '1234567', 3)]:
for i, x in enumerate(dice_gen(n, faces, loop_len)): pass
print(f'{n}-sided, markings {faces}, loop length {loop_len}:')
print(f'\t{i + 1}*{loop_len} solutions, e.g. {" > ".join(x)} > [loop]')
t, t0 = time(), t
print(f'\ttime: {t - t0:.4f} seconds\n')</syntaxhighlight>
{{out}}
<pre>4-sided, markings 1234, loop length 3:
1*3 solutions, e.g. 1144 > 1333 > 2224 > [loop]
time: 0.0000 seconds
4-sided, markings 1234, loop length 4:
1*4 solutions, e.g. 1144 > 1333 > 2233 > 2224 > [loop]
time: 0.0100 seconds
6-sided, markings 123456, loop length 3:
40666*3 solutions, e.g. 335666 > 555556 > 444666 > [loop]
time: 1.1459 seconds
6-sided, markings 1234567, loop length 3:
423063*3 solutions, e.g. 446777 > 666667 > 555777 > [loop]
time: 6.9465 seconds</pre>
===Alternative===
<syntaxhighlight lang="python">from collections import namedtuple
from itertools import permutations, product
from functools import lru_cache
Line 685 ⟶ 2,053:
print('\n More verbose comparison of last non_transitive result:')
print(' ', verbose_dice_cmp(non_trans[-1]))
print('\n ====')</
{{out}}
Line 743 ⟶ 2,111:
Out[45]: CacheInfo(hits=2148761, misses=1190, maxsize=None, currsize=1190)
</pre>
=={{header|R}}==
It would not be difficult to adapt this code to meet the stretch goal, but readability would suffer.
<syntaxhighlight lang="rsplus">findNonTrans <- function()
{
diceSet <- unique(t(apply(expand.grid(1:4, 1:4, 1:4, 1:4), 1, sort))) #By construction, each row is a unique dice.
winningDice <- function(X, Y) #Recall 'Example 1' in the task.
{
comparisonTable <- data.frame(X = rep(X, each = length(X)), Y = rep(Y, times = length(Y)))
rowWinner <- ifelse(comparisonTable["X"] > comparisonTable["Y"], "X",
ifelse(comparisonTable["X"] == comparisonTable["Y"], "-", "Y"))
netXWins <- sum(rowWinner == "X") - sum(rowWinner == "Y")
if(netXWins > 0) "X" else if(netXWins == 0) "Draw" else "Y"
}
rows <- seq_len(nrow(diceSet)) #Recall that each row of diceSet is a dice.
XvsAllY <- function(X) sapply(rows, function(i) winningDice(X, diceSet[i, ]))
winners <- as.data.frame(lapply(rows, function(i) XvsAllY(diceSet[i, ])),
row.names = paste("Y=Dice", rows), col.names = paste("X=Dice", rows), check.names = FALSE)
solutionCount <- 0
for(S in rows)
{
beatsS <- which(winners[paste("X=Dice", S)] == "Y") #Finds the indices of all T such that S<T.
beatsT <- lapply(beatsS, function(X) which(winners[paste("X=Dice", X)] == "Y")) #To begin finding U such that T<U.
beatenByS <- which(winners[paste("X=Dice", S)] == "X") #Finds the indices of all U such that S>U.
potentialU <- lapply(beatsT, function(X) intersect(X, beatenByS)) #Combining previous two lines.
nonEmptyIndices <- lengths(potentialU) != 0 #Most of potentialU is usually empty lists.
if(any(nonEmptyIndices)) #If any lists in potentialU are non-empty, then their entry is the index of a U with S>U & T<U.
{
solutionCount <- solutionCount + 1
diceUIndex <- potentialU[nonEmptyIndices][[1]] #Assumes that there is only one valid U for this S.
diceTIndex <- beatsS[nonEmptyIndices][[1]] #Finds the T corresponding to the chosen U.
cat("Solution", solutionCount, "is:\n")
output <- rbind(S = diceSet[S,], T = diceSet[diceTIndex,], U = diceSet[diceUIndex,])
colnames(output) <- paste("Dice", 1:4)
print(output)
}
}
}
findNonTrans()</syntaxhighlight>
{{out}}
<pre>Solution 1 is:
Dice 1 Dice 2 Dice 3 Dice 4
S 1 1 4 4
T 2 2 2 4
U 1 3 3 3
Solution 2 is:
Dice 1 Dice 2 Dice 3 Dice 4
S 1 3 3 3
T 1 1 4 4
U 2 2 2 4
Solution 3 is:
Dice 1 Dice 2 Dice 3 Dice 4
S 2 2 2 4
T 1 3 3 3
U 1 1 4 4</pre>
=={{header|Raku}}==
Thanks to Thundergnat for the nice "less-is-more" tweaks now the 4 dice portion takes around 10 (down from 17 ) minutes to run ..
{{trans|Go}}
<syntaxhighlight lang="raku" line># 20201225 Raku programming solution
my @dicepool = ^4 xx 4 ;
sub FaceCombs(\N, @dp) { # for brevity, changed to use 0-based dice on input
my @res = my @found = [];
for [X] @dp {
unless @found[ my \key = [+] ( N «**« (N-1…0) ) Z* .sort ] {
@found[key] = True;
@res.push: $_ »+» 1
}
}
@res
}
sub infix:<⚖️>(@x, @y) { +($_{Less} <=> $_{More}) given (@x X<=> @y).Bag }
sub findIntransitive(\N, \cs) {
my @res = [];
race for [X] ^+cs xx N -> @lot {
my $skip = False;
for @lot.rotor(2 => -1) {
{ $skip = True and last } unless cs[ @_[0] ] ⚖️ cs[ @_[1] ] == -1
}
next if $skip;
if cs[ @lot[0] ] ⚖️ cs[ @lot[*-1] ] == 1 { @res.push: [ cs[ @lot ] ] }
}
@res
}
say "Number of eligible 4-faced dice : ", +(my \combs = FaceCombs(4,@dicepool));
for 3, 4 {
my @output = findIntransitive($_, combs);
say +@output, " ordered lists of $_ non-transitive dice found, namely:";
.say for @output;
}</syntaxhighlight>
{{out}}
<pre>Number of eligible 4-faced dice : 35
3 ordered lists of 3 non-transitive dice found, namely:
[(1 1 4 4) (2 2 2 4) (1 3 3 3)]
[(1 3 3 3) (1 1 4 4) (2 2 2 4)]
[(2 2 2 4) (1 3 3 3) (1 1 4 4)]
4 ordered lists of 4 non-transitive dice found, namely:
[(1 1 4 4) (2 2 2 4) (2 2 3 3) (1 3 3 3)]
[(1 3 3 3) (1 1 4 4) (2 2 2 4) (2 2 3 3)]
[(2 2 2 4) (2 2 3 3) (1 3 3 3) (1 1 4 4)]
[(2 2 3 3) (1 3 3 3) (1 1 4 4) (2 2 2 4)]</pre>
=={{header|Wren}}==
{{libheader|Wren-sort}}
<
var fourFaceCombs = Fn.new {
Line 836 ⟶ 2,311:
it = findIntransitive4.call(combs)
System.print("\n%(it.count) ordered lists of 4 non-transitive dice found, namely:")
System.print(it.join("\n"))</
{{out}}
|