Tool to create or solve Latin squares, a square grid of NxN with N distinct symbols distributed without repetition in each row and column, ideal for lovers of logic and puzzles similar to sudoku.
Latin Square - dCode
Tag(s) : Number Games
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
A Latin square is a square grid of size $ n \times n $ filled with $ n $ distinct symbols (letters, numbers, or others). The main rule is as follows: each symbol must appear exactly once in each row and exactly once in each column.
Example: A 3x3 Latin square using the symbols $ 1,2,3 $:
| 1 | 2 | 3 |
| 3 | 1 | 2 |
| 2 | 3 | 1 |
There are several methods for generating Latin squares. Here is the shortest to explain:
1- Take an empty NxN grid,
Example:
| - | - | - |
| - | - | - |
| - | - | - |
2- Fill the first row with N symbols
Example:
| A | B | C |
| - | - | - |
| - | - | - |
3- Fill the next row by shifting all the symbols by 1 box to the right (and return to the beginning of the row if necessary)
Example:
| A | B | C |
| C | A | B |
| - | - | - |
4- Repeat step 3 until you reach the last row of the grid.
Example:
| A | B | C |
| C | A | B |
| B | C | A |
And voilĂ ! The square is thus valid but always identical, to obtain others:
5- Randomly swap 2 rows
6- Randomly swap 2 columns
7- Repeat steps 5 and 6 as many times as desired, the result is always a Latin square.
There are variants of Latin squares with additional constraints, such as diagonal Latin squares where each symbol must also be unique on both long diagonals of the grid.
Solving a Latin square can be summarized in one loop:
1- For each empty cell, determine the symbols that are not already present in the row and column.
2- If only one symbol is possible, the cell can be filled immediately with that symbol.
3- Repeat step 1 until the grid is complete.
For more complex grids, algorithmic methods are used, such as backtracking, which tests different possibilities while respecting the constraints.
The term Latin square was introduced by the Swiss mathematician Leonhard Euler in the 18th century. In his work, Euler used letters of the Latin alphabet as symbols to fill the grids.
The 36 Officers Problem, posed by Euler in 1782, asks whether it is possible to arrange 36 officers belonging to 6 different regiments and 6 different ranks in a 6 Ă— 6 grid such that each row and each column contains every regiment and every rank exactly once. This problem is equivalent to finding two orthogonal Latin squares of order 6 (also called Graeco-Latin squares). It was proven in the 20th century that such a configuration is impossible.
The best known variant is sudoku, which uses the same bases, but adds a constraint on blocks of 3x3 (and sometimes other constraints for irregular sudoku).
Ken-ken (kendoku) is also a Latin square with constraints of mathematical calculations.
The magic square is a distant mathematical variant which takes up the fact that the sum of the rows and the columns is always identical, but it is not limited to N distinct symbols.
The number of possible Latin squares increases rapidly with the size of $ N $. For $ N = 3 $ there are 12 different Latin squares, for $ N = 4 $ there are 576, then 161280 for 5, 812851200 for 6 etc. for larger sizes this number becomes exponential, see here
There is currently no simple formula that allows this number to be calculated directly for any $ N $.
Latin squares were systematically studied by the mathematician Leonhard Euler in 1782 (18th century). However, similar structures were probably used long before.
dCode retains ownership of the "Latin Square" source code. Any algorithm for the "Latin Square" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Latin Square" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Latin Square" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Latin Square" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Latin Square on dCode.fr [online website], retrieved on 2026-03-13,