How to resolve the algorithm Random Latin squares step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Random Latin squares step by step in the Picat programming language

Table of Contents

Problem Statement

A Latin square of size n is an arrangement of n symbols in an n-by-n square in such a way that each row and column has each symbol appearing exactly once. For the purposes of this task, a random Latin square of size n is a Latin square constructed or generated by a probabilistic procedure such that the probability of any particular Latin square of size n being produced is non-zero. Strict uniformity in the random generation is a hard problem and not a requirement of the task.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Random Latin squares step by step in the Picat programming language

Source code in the picat programming language

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.

main =>
  foreach(N in 1..6)
    Count = count_all(latin_square(N,_)),
    println(N=Count)
  end.

  

You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Chaos game step by step in the Lua programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the Nim programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the HicEst programming language