Books · Solving Puzzles through Mathematical Programming
Chapter 19
Euler’s Officers
In Leonhard Euler asked whether thirty-six officers, one of each combination of six ranks and six regiments, could be paraded in a six-by-six square so that every row and every column contained one officer of each rank and one of each regiment. He could not find an arrangement, and he conjectured that none exists. He was right about the thirty-six officers, though for a reason subtler than he could prove, and wrong about the wider claim he built on top of it. The same question with sixteen court cards, the ace, king, queen, and jack of each of the four suits, does have an answer, and a pretty one. Both are instances of a single object, the Graeco-Latin square, and the integer program that builds one is two Latin squares laid on the same grid. The construction is the subject of Chlond’s note in the INFORMS Transactions on Education puzzle column.
Two squares in one
A Latin square of order is an grid filled with symbols so that each symbol appears once in every row and once in every column. A Graeco-Latin square superimposes two of them. Each cell carries an ordered pair, a rank and a suit, say; the ranks form one Latin square, the suits form another, and the two are orthogonal, meaning every one of the possible rank-suit pairs occurs in exactly one cell. The card version, the sixteen-officers puzzle of Chlond’s note, asks for the sixteen court cards to be set in a four-by-four array so that no row or column repeats a rank or a suit, an orthogonal pair of order-four Latin squares, and adds a flourish: each of the two main diagonals must also carry every rank and every suit.
The programming model
Variables
For each cell introduce two integer variables, the rank and the suit placed in that cell.
Constraints
Each of the two squares is Latin: every row and every column of , and of , holds all symbols once, which is an all-different condition on each line. Orthogonality is the one extra requirement that makes the pair Graeco-Latin: the ordered pairs must all be distinct. Encoding a pair by the single number , this too is one all-different constraint, now over all cells: The sixteen-officers puzzle adds the diagonal flourish, the same all-different condition on each main diagonal of and of : There is no objective; any feasible filling is a Graeco-Latin square, and the model is infeasible exactly when none exists.
The solver
from ortools.sat.python import cp_model
def graeco(n, diagonals=False):
"""Two orthogonal n x n Latin squares (rank, suit), or None. With
diagonals=True each main diagonal also carries every symbol once."""
m = cp_model.CpModel()
a = [[m.NewIntVar(0, n - 1, "") for _ in range(n)] for _ in range(n)]
b = [[m.NewIntVar(0, n - 1, "") for _ in range(n)] for _ in range(n)]
for i in range(n):
m.AddAllDifferent(a[i]) # rows of a Latin
m.AddAllDifferent(b[i]) # rows of b Latin
m.AddAllDifferent([a[k][i] for k in range(n)]) # cols of a
m.AddAllDifferent([b[k][i] for k in range(n)]) # cols of b
m.AddAllDifferent([n * a[i][j] + b[i][j] # pairs distinct
for i in range(n) for j in range(n)])
if diagonals:
m.AddAllDifferent([a[i][i] for i in range(n)])
m.AddAllDifferent([b[i][i] for i in range(n)])
m.AddAllDifferent([a[i][n - 1 - i] for i in range(n)])
m.AddAllDifferent([b[i][n - 1 - i] for i in range(n)])
s = cp_model.CpSolver()
if s.Solve(m) not in (cp_model.OPTIMAL, cp_model.FEASIBLE):
return None
A = [[s.Value(a[i][j]) for j in range(n)] for i in range(n)]
B = [[s.Value(b[i][j]) for j in range(n)] for i in range(n)]
return A, B
Calling graeco(4, diagonals=True) returns an arrangement of the sixteen court cards, drawn in Figure 19.1: read the ranks alone and they form a Latin square, read the suits alone and they form another, no rank-suit pair is repeated, and each main diagonal too holds every rank and every suit. For the plain model returns nothing, because no Graeco-Latin square of order two exists; the two ranks and two suits cannot be paired four distinct ways inside a Latin structure.
The gap at six
Euler’s own case, , is the one the solver cannot help with. No order-six Graeco-Latin square exists, so the model is infeasible, but confirming that by search means ruling out an enormous space of fillings. Gaston Tarry certified it in by an exhaustive hand enumeration of the order-six Latin squares, that Euler’s thirty-six officers truly cannot be paraded; Thomas Clausen is reported to have reached the same conclusion independently as early as . The model on the page is faithful to the puzzle; it is simply asking a question whose negative answer took a person, not a solver, to certify.
Euler had conjectured more than the single case. He believed no Graeco-Latin square exists for any order , that is . The conjecture stood for nearly two centuries and then collapsed all at once. In and Raj Chandra Bose, Sharadchandra Shrikhande, and Ernest Parker constructed a Graeco-Latin square of order ten and then of every order beyond six, so the press named them “Euler’s spoilers.” The final tally is clean: a Graeco-Latin square of order exists for every except and .
Sources. The integer-programming construction follows Martin J. Chlond, Puzzle—Latin Square Puzzles, INFORMS Transactions on Education volume , number (), pages –. The thirty-six officers problem is Leonhard Euler, Recherches sur une nouvelle espèce de quarrés magiques (). The impossibility of order six was proved by Gaston Tarry, Le problème des officiers (–), and is reported, via Knuth, to have been settled independently by Thomas Clausen in . Euler’s broader conjecture was disproved by R. C. Bose, S. S. Shrikhande, and E. T. Parker, whose construction of Graeco-Latin squares for all orders greater than six appeared in –; the surviving exceptions are exactly and .