How magic squares work, and how to make your own

A magic square is a grid of the numbers 1 to n², each used once, arranged so that every row, every column and both main diagonals add up to the same total, called the magic constant. For a 3 × 3 square the constant is 15, for 4 × 4 it is 34, and in general it is n(n² + 1) ÷ 2. The magic square generator I built makes and checks squares of any order from 3 to 30, and this guide explains the methods behind it.

What makes a square magic

The order n is the number of cells along one side. A normal magic square uses each of 1, 2, … n² exactly once, and needs 2n + 2 lines (n rows, n columns, two diagonals) to share the same sum. If only the rows and columns match, it is called semi-magic. There is no normal 2 × 2 magic square, but every order from 3 upwards has one. Two handy facts: adding the same number to every cell keeps a square magic, and so does rotating or reflecting it, which turns one square into eight versions.

The magic constant

The constant follows from a simple count. The numbers 1 to n² add up to n²(n² + 1) ÷ 2. That total is shared equally between n rows, so each row must add to n(n² + 1) ÷ 2. For order 3: 3 × 10 ÷ 2 = 15. The sequence continues 34, 65, 111, 175 and 260 for orders 4 to 8. You can use the same reasoning to pin down the centre of a 3 × 3 square: the four lines through the centre (middle row, middle column and both diagonals) total 4 × 15 = 60, and between them they cover every cell once plus the centre three extra times, so 45 + 3 × centre = 60 and the centre must be 5.

The Lo Shu square

The oldest known magic square is the Chinese Lo Shu. Legend tells of a turtle emerging from the river Lo with this pattern on its shell:

  • 4 9 2
  • 3 5 7
  • 8 1 6

Every line sums to 15. Remarkably, it is essentially the only 3 × 3 magic square: every other one is a rotation or reflection of it, eight arrangements in all. A good classroom exercise is to give pupils an empty grid and the hint that 5 goes in the middle; the maths worksheet generator can supply addition practice to go with it.

Odd sizes: the Siamese method

For any odd order there is a simple recipe, known as the Siamese method or de la Loubère method. Simon de la Loubère brought it to France in 1688 after a diplomatic mission to Siam and published it in 1693; he had learnt it from a fellow traveller who had picked it up in India. The steps:

  • Put 1 in the middle cell of the top row.
  • Move one cell up and one to the right for each next number, wrapping round to the bottom row or the left column when you go off the edge.
  • If that cell is already filled, drop down one cell from the current number instead.

For order 3 this gives 8 1 6 / 3 5 7 / 4 9 2, which is the Lo Shu turned upside down. The same rule builds a 5 × 5 square with constant 65, a 7 × 7 with 175, and so on.

Even sizes and Dürer's square

Orders divisible by 4 have their own trick. Write 1 to n² in order, then replace the numbers on a fixed diagonal pattern with their complements, n² + 1 − v. For order 4 that gives 16 2 3 13 / 5 11 10 8 / 9 7 6 12 / 4 14 15 1. Swap its two middle columns and you get the square in Albrecht Dürer's 1514 engraving Melencolia I, whose bottom row shows 15 14, the year the engraving was made. The awkward orders are 6, 10, 14 and so on; the generator uses John Conway's LUX method for these, building them from an odd square of half the size.

How many magic squares exist

Counting rotations and reflections as the same square, there is 1 of order 3, 880 of order 4 and 275,305,224 of order 5; the number for order 6 is far larger. So the constructions above produce only a tiny family of the possibilities. If you enjoy number grids with strict rules, the sudoku puzzle uses a similar kind of reasoning, and Pascal's triangle: the patterns hidden in a triangle of numbers and The Collatz conjecture: a simple rule nobody can prove explore more recreational maths.

Sources and further reading

The claims in this guide rest on these references, which were checked when the guide was last updated. Spotted an error? The contact page says how to report it.

  1. Magic square — Wikipedia (Lo Shu, Dürer, enumeration)
  2. Siamese method — Wikipedia
  3. Magic Square — Wolfram MathWorld

Try the tool

Frequently asked questions

What is the magic constant of a 3x3 magic square?

15. In general a normal magic square of order n has a magic constant of n(n squared plus 1) divided by 2.

Is there a 2x2 magic square?

No. There is no normal 2 by 2 magic square using the numbers 1 to 4, but every order from 3 upwards has one.

What number goes in the middle of a 3x3 magic square?

5. The four lines through the centre add to 60, which forces the centre to be 5.

How many 3x3 magic squares are there?

Only one, the Lo Shu square, if rotations and reflections are counted as the same; there are 8 arrangements in all.