Jump to content

Latin Squares in reduced form: Difference between revisions

Realize in MiniZinc
(Realize in MiniZinc)
Line 627:
Order 6: Size 9408 x 6! x 5! => Total 812851200</pre>
 
=={{header|MiniZinc}}==
===The Model (lsRF.mnz)===
<lang MiniZinc>
%Latin Squares in Reduced Form. Nigel Galloway, September 5th., 2019
include "alldifferent.mzn";
int: N;
array[1..N,1..N] of var 1..N: p; constraint forall(n in 1..N)(p[1,n]=n /\ p[n,1]=n);
constraint forall(n in 1..N)(alldifferent([p[n,g]|g in 1..N])/\alldifferent([p[g,n]|g in 1..N]));
</lang>
===The Tasks===
;displaying the four reduced Latin Squares of order 4
<lang MiniZinc>
include "lsRF.mzn";
output [show_int(1,p[i,j])++
if j == 4 then
if i != 4 then "\n"
else "" endif
else "" endif
| i,j in 1..4 ] ++ ["\n"];
</lang>
When the above is run using minizinc --all-solutions -DN=4 the following is produced:
{{out}}
<pre>
1234
2143
3421
4312
----------
1234
2143
3412
4321
----------
1234
2413
3142
4321
----------
1234
2341
3412
4123
----------
==========
</pre>
;counting the solutions
minizinc.exe --all-solutions -DN=5 -s lsRF.mzn produces the following:
<pre>
.
.
.
p = array2d(1..5, 1..5, [1, 2, 3, 4, 5, 2, 3, 4, 5, 1, 3, 1, 5, 2, 4, 4, 5, 2, 1, 3, 5, 4, 1, 3, 2]);
----------
p = array2d(1..5, 1..5, [1, 2, 3, 4, 5, 2, 3, 5, 1, 4, 3, 5, 4, 2, 1, 4, 1, 2, 5, 3, 5, 4, 1, 3, 2]);
----------
p = array2d(1..5, 1..5, [1, 2, 3, 4, 5, 2, 3, 4, 5, 1, 3, 5, 2, 1, 4, 4, 1, 5, 2, 3, 5, 4, 1, 3, 2]);
----------
==========
%%%mzn-stat: initTime=0.057
%%%mzn-stat: solveTime=0.003
%%%mzn-stat: solutions=56
%%%mzn-stat: variables=43
%%%mzn-stat: propagators=8
%%%mzn-stat: propagations=960
%%%mzn-stat: nodes=111
%%%mzn-stat: failures=0
%%%mzn-stat: restarts=0
%%%mzn-stat: peakDepth=7
%%%mzn-stat-end
%%%mzn-stat: nSolutions=56
</pre>
and minizinc.exe --all-solutions -DN=6 -s lsRF.mzn produces the following:
<pre>
.
.
.
p = array2d(1..6, 1..6, [1, 2, 3, 4, 5, 6, 2, 4, 5, 6, 3, 1, 3, 1, 4, 2, 6, 5, 4, 6, 2, 5, 1, 3, 5, 3, 6, 1, 2, 4, 6, 5, 1, 3, 4, 2]);
----------
p = array2d(1..6, 1..6, [1, 2, 3, 4, 5, 6, 2, 1, 4, 6, 3, 5, 3, 4, 5, 2, 6, 1, 4, 6, 2, 5, 1, 3, 5, 3, 6, 1, 2, 4, 6, 5, 1, 3, 4, 2]);
----------
==========
%%%mzn-stat: initTime=0.003
%%%mzn-stat: solveTime=6.669
%%%mzn-stat: solutions=9408
%%%mzn-stat: variables=58
%%%mzn-stat: propagators=10
%%%mzn-stat: propagations=179635
%%%mzn-stat: nodes=19035
%%%mzn-stat: failures=110
%%%mzn-stat: restarts=0
%%%mzn-stat: peakDepth=17
%%%mzn-stat-end
%%%mzn-stat: nSolutions=9408
</pre>
The only way to complete the tasks requirement to produce a table is with another language. Ruby has the ability to run an external program, capture the output, and text handling ability to format it to this tasks requirements. Othe scripting languages are available.
=={{header|Phix}}==
A Simple backtracking search.<br>
2,172

edits

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