applets.

Magic Squares

← All apps

About Magic Squares

A magic square holds the numbers 1, 2, 3 … up to n² in an n × n grid so that every row, every column and both long diagonals add up to the same total, the magic total n(n² + 1) ÷ 2: 15 for a 3 × 3, 34 for a 4 × 4, 65 for a 5 × 5. The sums sit round the edge of the plate and fill with gold when a line comes out right.

BUILD picks a size from 3 to 12 and the method that suits it, then fills the square one step at a time: NEXT and BACK (or the arrow keys), START (or Space; PAUSE and RESUME) at the speed you choose, END to see it finished. SPIRAL fills the same grid the way the Python script spiralmatrix.py this app grew from does: 1, 2, 3 … round and round from the top-left corner. It looks orderly but it isn't magic: its top row is just 1 to n.

FAMOUS shows four old squares and the extra patterns hidden in them. CHECK lets you type your own (3 × 3 to 8 × 8) and says whether it is magic, semi-magic (rows and columns right, diagonals not) or not magic, and which lines are out and by how much. PUZZLE removes some numbers from a 3 × 3 or 4 × 4 for you to put back; there is always exactly one answer.

THE STORY

A Chinese legend tells of a turtle that came out of the Luo river in the time of Emperor Yu, the tamer of the great flood, with a pattern of dots on its shell: the Lo Shu, the 3 × 3 square of 1 to 9. The numbers themselves are set out in Chinese writings by the early centuries AD. Arab and Persian mathematicians wrote treatises on building larger squares from around the 10th century, and in India a 4 × 4 that is also magic along its broken diagonals was carved into the Parshvanatha temple at Khajuraho, usually dated to around the 10th century.

In Europe, Albrecht Dürer put a 4 × 4 into his engraving Melencolia I in 1514, with the year in its bottom row, and Cornelius Agrippa's Three Books of Occult Philosophy (1533) gave squares of 3 × 3 up to 9 × 9, one for each of the seven planets then known. Bernard Frénicle de Bessy listed all 880 different 4 × 4 squares; the list was published in 1693, after his death. Simon de la Loubère, the French envoy to Siam in 1687–88, brought back the staircase method for odd squares and described it in his account of the kingdom. Benjamin Franklin made squares to pass the time as clerk of the Pennsylvania Assembly, and described his 8 × 8 and 16 × 16 in a letter to his friend Peter Collinson in London.

How many are there? Counting a square and its turns and mirror images as one: 1 of size 3, 880 of size 4, and 275,305,224 of size 5, counted by computer by Richard Schroeppel in 1973 and published by Martin Gardner in 1976. The 6 × 6 count took until 2024, when Hidetoshi Mino found by computer that there are 17,753,889,197,660,635,632, about 1.8 × 10¹⁹.

HOW IT WORKS

Odd sizes, the staircase: put 1 in the middle of the top row. Each next number goes one up and one right, wrapping round from top to bottom and from right to left. When that square is already taken, drop straight down one instead. Each "lap" of n numbers (1 to n, then n + 1 to 2n, and so on) climbs a slanting line of its own, so every row and column gets one number from every lap, each at a different place in its lap. One long diagonal does the same; the other is the middle lap itself, whose numbers average out to the right total. That is why it works.

4, 8, 12, diagonal swaps: write 1 to n² in order along the rows. Cut the square into 4 × 4 blocks and, on the two diagonals of each block, swap every number x for its partner n² + 1 − x. Half of every line is swapped, and swapping turns the small numbers big and the big ones small in exactly the right amounts. Dürer's square is this 4 × 4 with its middle two columns swapped.

6 and 10: these are the hard ones. LUX, John Horton Conway's method: think of the square as 2 × 2 blocks, label the top rows of blocks L, one row U and the rest X (swapping the middle U with the L above it), walk round the blocks with the staircase and write four numbers into each block in the shape of its letter. QUARTERS, Ralph Strachey's method (1918): fill the four quarters with odd staircase squares (1–9 top left, 10–18 bottom right, 19–27 top right, 28–36 bottom left for a 6 × 6), then swap a few columns between the top and bottom quarters to even out the rows.

The checker adds every line. If the numbers are not 1 to n², the target is their total ÷ n, the only value all the rows could share. Pandiagonal means the broken diagonals (that run off one side and come back on the other) add up too; associative means numbers opposite each other across the centre add up to n² + 1 (or, when the numbers are not 1 to n², to twice their average); most-perfect (sizes 4, 8, 12…) means pandiagonal, every 2 × 2 block adds up to 2(n² + 1), and numbers n/2 apart along a diagonal add up to n² + 1.

Puzzles start from a random magic square, found by a search that tries the numbers in a shuffled order, then take numbers away one at a time, keeping each removal only if a full search still finds exactly one way to finish. The same search, run on an empty 4 × 4, finds all 7,040 squares: Frénicle's 880, each in its 8 turns and mirror images.

FURTHER WATCHING AND READING

Size
Method

Speed

How many are there? (turns and mirror images count once)
3 × 31the Lo Shu
4 × 4880Frénicle de Bessy, 1693
5 × 5275,305,224Richard Schroeppel, 1973
6 × 6≈ 1.8 × 1019counted by computer in 2024
7 × 7 up?far too many to count

Keys: ← → step, Space plays or pauses, Home and End.