A Latin Square is in its reduced form if the first row and first column contain items in their natural order. The order n is the number of items. For any given n there is a set of reduced Latin Squares whose size increases rapidly with n. g is a number which identifies a unique element within the set of reduced Latin Squares of order n. The objective of this task is to construct the set of all Latin Squares of a given order and to provide a means which given suitable values for g any element within the set may be obtained.

For a reduced Latin Square the first row is always 1 to n. The second row is all Permutations/Derangements of 1 to n starting with 2. The third row is all Permutations/Derangements of 1 to n starting with 3 which do not clash (do not have the same item in any column) with row 2. The fourth row is all Permutations/Derangements of 1 to n starting with 4 which do not clash with rows 2 or 3. Likewise continuing to the nth row.

Demonstrate by:

 displaying the four reduced Latin Squares of order 4.
 for n = 1 to 6 (or more) produce the set of reduced Latin Squares; produce a table which shows the size of the set of reduced Latin Squares and compares this value times n! times (n-1)! with the values in OEIS A002860.


F dList(n, =start)
   start--
   V a = Array(0 .< n)
   a[start] = a[0]
   a[0] = start
   a.sort_range(1..)
   V first = a[1]
   [[Int]] r
   F recurse(Int last) -> N
      I (last == @first)
         L(v) @a[1..]
            I L.index + 1 == v
               R
         V b = @a.map(x -> x + 1)
         @r.append(b)
         R
      L(i) (last .< 0).step(-1)
         swap(&@a[i], &@a[last])
         @recurse(last - 1)
         swap(&@a[i], &@a[last])
   recurse(n - 1)
   R r
 
F printSquare(latin, n)
   L(row) latin
      print(row)
   print()
 
F reducedLatinSquares(n, echo)
   I n <= 0
      I echo
         print(‘[]’)
      R 0
   E I n == 1
      I echo
         print([1])
      R 1
 
   V rlatin = [[0] * n] * n
   L(j) 0 .< n
      rlatin[0][j] = j + 1
 
   V count = 0
   F recurse(Int i) -> N
      V rows = dList(@n, i)
 
      L(r) 0 .< rows.len
         @rlatin[i - 1] = rows[r]
         V justContinue = 0B
         V k = 0
         L !justContinue & k < i - 1
            L(j) 1 .< @n
               I @rlatin[k][j] == @rlatin[i - 1][j]
                  I r < rows.len - 1
                     justContinue = 1B
                     L.break
                  I i > 2
                     R
            k++
         I !justContinue
            I i < @n
               @recurse(i + 1)
            E
               @count++
               I @echo
                  printSquare(@rlatin, @n)
 
   recurse(2)
   R count
 
print("The four reduced latin squares of order 4 are:\n")
reducedLatinSquares(4, 1B)
 
print(‘The size of the set of reduced latin squares for the following orders’)
print("and hence the total number of latin squares of these orders are:\n")
L(n) 1..6
   V size = reducedLatinSquares(n, 0B)
   V f = factorial(n - 1)
   f *= f * n * size
   print(‘Order #.: Size #<4 x #.! x #.! => Total #.’.format(n, size, n, n - 1, f))