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.

Last updated · Method and sources below

Magic square solver

Magic constant for this order: 15. It is worked out from the order, so you do not enter it.

Known numbers

Click a cell and type a number.

Grid of known numbers. Leave unknown cells empty.

Whole numbers from 1 to 9, each at most once. Leave a cell empty if it is unknown.

Counts all solutions; displays up to 24. Rotations and reflections count separately.

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

Solving by hand

  1. Work out M from the formula, or read it from the tool.
  2. Find a row, column or diagonal with exactly one blank. Fill it with M minus the sum of the other cells.
  3. 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

  1. 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).
  2. “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.

Report an error