Magic square solver and calculator
Type the numbers you already know into a 3 × 3 or 4 × 4 grid. The solver lists every normal magic square that fits, with the magic constant and a check of each line.
Completions found
magic squares fit the numbers entered
Magic constant: (every row, column and diagonal)
Enter at least one number, then press Solve. The result appears here.
How this is calculated
The solver looks for normal magic squares: the numbers 1 to n², each used exactly once, with every row, column and both diagonals equal to
M = n(n² + 1) / 2
For n = 3 that is 3 × 10 / 2 = 15, and for n = 4 it is 4 × 17 / 2 = 34.
It then fills the empty cells one at a time and backtracks. A number is rejected if it is already used, if it pushes a line above M, or if it completes a line that does not equal M. It also rejects a placement when the cells left in a line could not possibly make up the missing total. Every grid that survives is a completion, and all of them are counted, so the figure is exact.
Worked check on the 4 × 4 square in Dürer’s Melencolia I, with its main diagonal cleared: the row 16, 3, 2, 13 leaves only the first cell blank in the top row, so it must be 34 − 18 = 16. In the same way the other blanks are 10, 7 and 1. The solver confirms there is exactly one completion.
Limits
- Orders 3 and 4 only. Order 5 has 275,305,224 distinct squares, far too many to list.
- Normal squares only: numbers outside 1 to n², repeated numbers and decimals are rejected with an error message.
- For 3 × 3 there are 8 squares (one square and its rotations and reflections); for 4 × 4 there are 7,040, which is 880 squares counted with their 8 rotations and reflections. Only the first 24 completions are drawn, and the count is always the full count.
- There is no 2 × 2 magic square. The numbers 1 to 4 would need a constant of 5, which forces two cells to hold the same number.
Solving by hand
- Work out M from the formula, or read it from the tool.
- Find a row, column or diagonal with exactly one blank. Fill it with M minus the sum of the other cells.
- Repeat until nothing is blank.
If every line has two or more blanks, hand filling stalls and the solver is the quicker route. For a 3 × 3 grid there is also a shortcut: the centre must be 5, explained in the 3 × 3 magic square guide. For a 4 × 4 grid see the 4 × 4 magic square guide.
Sources
- Weisstein, Eric W. “Magic Square.” MathWorld, A Wolfram Resource. mathworld.wolfram.com/MagicSquare.html (constant formula and the counts 8, 880 and 275,305,224).
- “Magic square.” Wikipedia. en.wikipedia.org/wiki/Magic_square (counts by order; no order-2 square).
Reading the answer
The big figure is the number of different magic squares that agree with every number you typed. Zero is a valid answer: it means no normal magic square fits, so at least one entry is wrong. One means the grid is fully determined. A larger figure means you need more clues to pin one square down.
Each completion is drawn with its row, column and diagonal sums, so you can check it yourself. To practise rather than solve, try the magic square puzzle, or return to the magic square generator to build a bigger grid.