A Latin square is a square that has no repeated symbols in either the X or Y columns. For example:
ABCD DABC CDAB BCDA
is one such square. Notice how every column and row contains a permutation of the same 4 letters.
However, our Latin square has a problem: If I were to rotate the second row (
DABC) 1 to the left, I'd end up with
ABCD, which is identical to the permutation above it. If it is impossible to rotate any 1 column/row and obtain another column/row, then we consider the square to be rotation safe.
ABCD BDAC CADB DCBA
is rotation safe. The grid has the following properties:
- Point [0,N] uses the Nth symbol
- Point [0,N] and [N,0] are always the same symbol. (I'd like to also say that [x,y] and [y,x] are also always the same letter, but I can't prove it)
Your task is to print out 1 rotation-safe Latin square, when passed N. I don't care if you output letters, numbers, a list, or a 2D array. If you use numbers, the top column and row must be
0,1,2,3,... (in that order). If you use letters, then it must be
For example, if your input was 4, you should either print:
0,1,2,3 0,1,2,3 1,3,0,2 or 1,0,3,2 2,0,3,1 2,3,1,0 3,2,1,0 3,2,0,1
There are no rotation-safe Latin squares of size less than 4. I don't care what your program does if N is less than 4. For the curious, the number of rotation-safe squares is (starting at 4):
2,5,5906,(too long to calculate)
This is a code-golf, so try to make answers as short as possible in your favorite language!