Random Latin squares: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 2,159:
ZL6K5NFVMCYbXfA8Oe43g7dUcDa9JTEQGSWB1PR2IH
</pre>
=={{header|Picat}}==
Using constraint modelling.
The used labeling (search strategy) is:
* <code>ff</code>: select a variable to label using the first fail strategy
* <code>rand_val</code>: select a random (but valid) value.
Normally a non-random value strategy is preferred such as <code>up</code>, <code>split</code>, or <code>updown</code>, but the task requires random solutions.
<lang Picat>main =>
_ = random2(), % random seed
N = 5,
foreach(_ in 1..2)
latin_square(N, X),
pretty_print(X)
end,
% A larger random instance
latin_square(62,X),
pretty_print(X).
% Latin square
latin_square(N, X) =>
X = new_array(N,N),
X :: 1..N,
foreach(I in 1..N)
all_different([X[I,J] : J in 1..N]),
all_different([X[J,I] : J in 1..N])
end,
% rand_val for randomness
solve($[ff,rand_val],X).
pretty_print(X) =>
N = X.len,
Alpha = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
foreach(I in 1..N)
foreach(J in 1..N)
if N > 20 then
printf("%w",Alpha[X[I,J]])
else
printf("%2w ",X[I,J])
end
end,
nl
end,
nl.</lang>
{{out}}
<pre> 5 1 3 4 2
1 2 4 3 5
4 5 2 1 3
2 3 1 5 4
3 4 5 2 1
5 2 3 4 1
3 4 5 1 2
2 5 1 3 4
4 1 2 5 3
1 3 4 2 5
9PRJhliyKjwkDVt60spfbz3aq8UmLMcSx1ed7nWIogENGr4uXHAvOQBTCYFZ52
CQEAorTdMWiDle2nXNg8YptFxBZwySGzURIPKubhHOJV49jmcqaLkfs06351v7
DEPvH2rfj7AVpyqx1ChMBQUGWsIoXzTedJFn3RL5YkZbK8Ni6cOmw90gauSlt4
wRSKyHhLiZeMztGcaETodVQprFjAvqN56Cmfxklg7UDYnOu1I49s8WPBJ032Xb
SwCqxBOrFsjpYKb360dPzaMDIHgTRefQZNuUk4vV8mytE5GhWXLAno7219Jilc
QcAaj62gZKf8TWPXY9Cv0yphwxDsF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YWdZCT71GzRFAg6rhtI2xQa8nKUkvwV4usfNMD
sB830FI92ipdv7MmgrfZwJGSOnQCaHDKb6l41jXERehATYVUzPxWt5NyLkcoqu
dy1DU3fZTmqiorFlw4kaHBx5e2GYVLhWKSXIC7RQgbzu6P8njtJ9MOAsNp0Ecv
e2INRZoPBX1jQhmD8i35vrut6E7zWcnGOL0HwYglkqACFdfV9M4psUSxbaTKJy
4CDH2J83Ehvl7RyTSpWUqgz9PYF61mQOjGd5oZNecVLkBKxbw0fXirMuItnasA
CPU time 0.048 seconds.</pre>
===Number of solutions===
The number of solutions for a Latin square of size N is the OEIS sequence [https://oeis.org/A002860 A002860: Number of Latin squares of order n; or labeled quasigroups]. Here we check N = 1..6 which took 18min54s.
<lang Picat>main =>
foreach(N in 1..6)
Count = count_all(latin_square(N,_)),
println(N=Count)
end.</lang>
{{out}}
<pre>1 = 1
2 = 2
3 = 12
4 = 576
5 = 161280
6 = 812851200
CPU time 1134.36 seconds.
</pre>
=={{header|Python}}==
|