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.


#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Latin_Squares_in_reduced_form
use warnings;
 
my $n = 0;
my $count;
our @perms;
 
while( ++$n <= 7 )
  {
  $count = 0;
  @perms = perm( my $start = join '', 1 .. $n );
  find( $start );
  print "order $n size $count total @{[$count * fact($n) * fact($n-1)]}\n\n";
  }
 
sub find
  {
  @_ >= $n and return $count += ($n != 4) || print join "\n", @_, "\n";
  local @perms = grep 0 == ($_[-1] ^ $_) =~ tr/\0//, @perms;
  my $row = @_ + 1;
  find( @_, $_ ) for grep /^$row/, @perms;
  }
 
sub fact { $_[0] > 1 ? $_[0] * fact($_[0] - 1) : 1 }
 
sub perm
  {
  my $s = shift;
  length $s <= 1 ? $s :
    map { my $f = $_; map "$f$_", perm( $s =~ s/$_//r ) } split //, $s;
  }