On the last day before the Dashain holidays, your maths teacher draws a noughts-and-crosses grid on the board. “Put the numbers 1 to 9 in these boxes, each one once,” she says, “so that every row, every column and both diagonals add up to the same number. The first to do it can go home early.”
It sounds easy. Most people fill the first row in a few seconds, and then stay stuck for a long time.
Squares like this are very old. At the entrance of a Jain temple at Khajuraho, in India, someone carved a bigger one long ago: sixteen numbers in a 4 × 4 grid, every row, column and diagonal adding to 34, and more besides. You will meet it later. Start with the teacher’s 3 × 3.
Guess before you try. If it can be done at all, what must every line add up to?
Pick an answer to see why.
Try it
You found the target, 15, without a single try. Before searching for something, it often pays to ask what it would have to look like. The same kind of thinking can tell you much more.
What goes in the middle?
Four lines pass through the middle box: the middle row, the middle column and the two diagonals. Add up all four lines. Each line makes 15, so together they make 4 × 15 = 60.
Now count which boxes went into that 60. Every box except the middle is on exactly one of the four lines, so its number was counted once. The middle box is on all four, so its number was counted four times: once like the others, and three more times.
So 60 is all nine numbers once (45), plus three extra copies of the middle number. That leaves 60 − 45 = 15 for the three extra copies, so the middle number is 15 ÷ 3 = 5. It has to be 5. No other number can work there.
Corners or edges?
With 5 in the middle, each line through it needs two more numbers adding to 10, so the numbers sit in opposite pairs: 1 opposite 9, 2 opposite 8, 3 opposite 7, 4 opposite 6. Now ask where the 1 can go. There are two kinds of box left: the four corners and the four edges (the middle of each side).
Suppose 1 is in a corner. Three lines pass through that corner: its row, its column and a diagonal. The diagonal goes through the 5, so the opposite corner is 9. The row and the column each need two more numbers adding to 15 − 1 = 14, without using 5 or 9. From the numbers left, only one pair adds to 14: 6 + 8. One pair can’t fill two lines. So 1 can’t be in a corner: it must be on an edge, with 9 on the opposite edge.
The same argument works for 9 and for 3 and 7. So which numbers must go in the four corners?
Pick an answer to see why.
Only one square
Now the square almost builds itself. Whatever square you have, you can turn it so that the 9 is in the top edge. Do that. The top row needs two more numbers adding to 15 − 9 = 6, from the corners, and only 2 + 4 does it. Flip the square over if you need to, so that the 2 is on the left. Then the 5 in the middle fixes everything else: 1 goes in the bottom edge, the 8 and 6 go opposite the 2 and the 4, and 3 and 7 fill the side edges. Not one choice was left.
So every 3 × 3 square of 1 to 9 that works is really the same square: turn it a quarter turn, a half turn or three quarters, or flip it over, and you get the others. That makes 8 squares in all, and only one different one. A computer that tried all 362,880 ways of putting 1 to 9 in the grid found exactly those 8, and no others.
What’s going on
Magic squares and their magic number
A grid of numbers in which every row, every column and both main diagonals add to the same total is called a magic square. The number of boxes along a side is its order (the teacher’s square has order 3), and the total that every line makes is the magic constant (also called the magic sum). A magic square that uses each of the numbers 1, 2, 3 … up to the number of boxes, once each, is called normal. The square you found is the normal magic square of order 3, and you have proved it is the only one, up to turning and flipping.
The magic constant of any normal magic square can be found the way you found 15, without building the square. A 4 × 4 square holds 1 to 16. Pair them from the ends: 1 + 16, 2 + 15, 3 + 14, and so on to 8 + 9. That is 8 pairs, each making 17, so the total is 8 × 17 = 136. Four rows share it, so each row makes 136 ÷ 4 = 34: the number on the temple at Khajuraho.
The same idea works for any order n, even when the count of numbers is odd and one is left without a partner. Write the numbers 1 to in a line, and the same numbers backwards underneath. Each of the columns adds to , so the two lines together make , and one line, the total of all the numbers, is half of that. Share it among n rows:
| Order n | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|
| Numbers used | 1–9 | 1–16 | 1–25 | 1–36 | 1–49 | 1–64 | 1–81 |
| Total of all | 45 | 136 | 325 | 666 | 1,225 | 2,080 | 3,321 |
| Magic constant | 15 | 34 | 65 | 111 | 175 | 260 | 369 |
The magic constant is just the fair share: the total of all the numbers divided equally among the rows. A magic square is a way of sharing the numbers out so fairly that every row gets the same, and every column and diagonal too.
What must every line of a normal 5 × 5 magic square add up to?
Pick an answer to see why.
No 2 × 2 magic square
Order 1 is a single box, which is magic by default. Order 2 is impossible. Call the four numbers a, b in the top row and c, d below. The top row and the left column must give the same total: a + b = a + c. Take a from both, and b = c. So two boxes would have to hold the same number. Every order from 3 up has normal magic squares; 2 is the only gap.
Changing a magic square without breaking it
Take the square you found and add 5 to every box. Every line has 3 boxes, so every line grows by 3 × 5 = 15, to 30. It is still magic, now using the numbers 6 to 14. Double every box instead and every line doubles, to 30, using 2, 4 … 18. Or take every number from 10: 1 becomes 9, 2 becomes 8, and so on; every line becomes 30 − 15 = 15 again, and you get the same square turned half way round.
So once you have one magic square, you can make many. With letters, as in Grade 7’s algebraic expressions, you can describe them all at once. Choose any three numbers c, a and b, and fill the grid like this:
| c − b | c + a + b | c − a |
|---|---|---|
| c − a + b | c | c + a − b |
| c + a | c − a − b | c + b |
Add the top row: (c − b) + (c + a + b) + (c − a). The as cancel and the bs cancel, leaving 3c. Do the same for any line you like: the as and bs always cancel, and every line adds to 3c. With c = 5, a = 3 and b = 1 the grid is 4, 9, 2 / 3, 5, 7 / 8, 1, 6: your square again, turned. And every 3 × 3 magic square of 1 to 9 fits this pattern; a computer checked all 8.
Bigger squares
Finding a 3 × 3 magic square by trying is hard work; finding a 7 × 7 one that way would take you forever. Mathematicians found methods: rules you can follow, one number after another, that always end in a magic square.
The staircase, for odd sizes
Here is a rule that works for every odd order, 3, 5, 7, 9 and up:
- Put 1 in the middle box of the top row.
- Put each next number one box up and one box to the right, like climbing a staircase.
- If that takes you off the top, come in at the bottom of that column. If it takes you off the right side, come in at the left end of that row.
- If the box you reach already has a number in it (or you go off the top right corner), put the next number in the box just below the one you just filled, and carry on.
Every size gives a magic square: 15 for 3 × 3, 65 for 5 × 5, 175 for 7 × 7. And the 3 × 3 square it makes is the one you already know, flipped over.
Why does such a simple rule work? Notice that the staircase drops down exactly after every fifth number in the 5 × 5 square (after 5, 10, 15, 20): the numbers come in runs of five, each run climbing a diagonal. So split every number into two parts: which run it is in (0 for 1 to 5, 1 for 6 to 10 … 4 for 21 to 25), and its place in that run (1 to 5). Then each number is 5 × run + place: 17 is in run 3, place 2, and 5 × 3 + 2 = 17.
Now look at Figure 9.2. In the grid of runs, every row and every column has each of 0, 1, 2, 3 and 4 exactly once. In the grid of places, every row and column has each of 1, 2, 3, 4 and 5 exactly once. So every row and column of the square adds to
The diagonals work too: one diagonal of the runs is 2, 2, 2, 2, 2, which adds to 10 just like 0 + 1 + 2 + 3 + 4, and the others hold each value once. Each run climbs a slanting line and drops one row at its end, and that is what spreads the runs and the places evenly across the rows and columns. Splitting each number in two like this is how de la Loubère, who brought the method to France, explained it himself.
Turning the diagonals, for 4 × 4
For a 4 × 4 square there is a trick that looks like magic itself. Write 1 to 16 in order, four to a row. Then, only on the two diagonals, replace each number n by 17 − n: 1 becomes 16, 6 becomes 11, and so on. That is the same as reading each diagonal backwards.
Why does it work? Look at any row of 1 to 16 in order, say 5, 6, 7, 8. Its two outer numbers add to the same as its two inner ones: 5 + 8 = 6 + 7 = 13. (The outer ones are one less and one more than the inner ones.) The row pairs go 1 + 4 = 5, then 13, 21 and 29.
Turning the diagonals swaps each row’s diagonal pair for the matching pair from the row on the opposite side. The top row keeps its inner pair, 2 + 3 = 5, and gets the bottom row’s outer pair, 16 + 13 = 29: 5 + 29 = 34. The second row keeps its outer pair, 5 + 8 = 13, and gets the third row’s inner pair, 11 + 10 = 21: 13 + 21 = 34. The columns work in the same way, and each diagonal was already 1 + 6 + 11 + 16 = 34 before it was turned round.
Now swap the two middle columns of the right-hand square. Each row keeps its four numbers, so each row still makes 34, and so does each column; checking the diagonals shows they still make 34 too. The result is 16, 3, 2, 13 / 5, 10, 11, 8 / 9, 6, 7, 12 / 4, 15, 14, 1: the magic square that the German artist Albrecht Dürer put into his engraving Melencolia I in 1514. He even arranged it so that the date appears in the middle of the bottom row. His square has more than it needs: the four corners make 34 (16 + 13 + 4 + 1), so do the four middle boxes, and so does each quarter of the square.
The temple square
Here is the square carved at the Parshvanatha temple in Khajuraho:
| 7 | 12 | 1 | 14 |
|---|---|---|---|
| 2 | 13 | 8 | 11 |
| 16 | 3 | 10 | 5 |
| 9 | 6 | 15 | 4 |
Every row, column and diagonal makes 34, like Dürer’s. But this one is more magic still. Imagine the square printed over and over, side by side and one above another, like tiles on a floor. Then a diagonal can keep going past the edge and come back in on the other side. These broken diagonals make 34 too: 12 + 8 + 5 + 9 = 34 (the 12 in the top row, then down and right to the 8 and the 5, then off the right edge and back in at the left, to the 9). All eight diagonals of the tiled floor make 34. So does every block of four boxes in a 2 × 2 square, anywhere, even one that wraps round an edge; and any two numbers two steps apart along a diagonal add to 17. Squares like this are called most-perfect.
The temple square also hides a trick for making a magic square with any total you like, from 34 up. Look at where its four biggest numbers, 13, 14, 15 and 16, are. There is one in every row, one in every column, and one on each of the two diagonals. So if you add the same amount to just those four, every line grows by that amount, and every line still has the same total.
To get a total of 100, add 100 − 34 = 66 to the four tinted numbers. Below 34 the trick fails: the four tinted numbers would have to shrink, and with 33 the 13 becomes 12, which is already in the square.
How many are there?
You proved there is only one normal magic square of order 3 (counting turns and flips as the same). For bigger orders the numbers explode:
| Order | Different normal magic squares (turns and flips counted once) | First counted |
|---|---|---|
| 3 | 1 | you, in this topic |
| 4 | 880 | by Bernard Frénicle de Bessy, published in 1693 |
| 5 | 275,305,224 | by Richard Schroeppel, with a computer, in 1973 |
| 6 | 17,753,889,197,660,635,632 | by Hidetoshi Mino, with computers, in 2024 |
| 7 | nobody knows, as of 2026 |
Counting turns and flips separately, every order-4 square comes in 8 versions, so there are 880 × 8 = 7,040 in all. A computer program written for this topic built every one of them, and found exactly 7,040; 384 of them are most-perfect, like the temple square.
A story from maths
Where this is used
Honestly, magic squares themselves have almost no practical use today. For most of their history they were puzzles, works of art and lucky charms (talismans, which of course they are not). Varahamihira’s perfume square is a rare example of one doing a real job.
Their close cousins do real work. A Latin square is a grid in which every row and every column holds each symbol exactly once, like the grids of runs and places in Figure 9.2.
Questions
Fill in the rest of this magic square of 1 to 9. The top row starts 8, 1.
Hint
Answer
A 3 × 3 magic square is made of nine different numbers, not 1 to 9. Its middle number is 12. What does every line add up to?
Pick an answer to see why.
Can a 3 × 3 magic square of nine different whole numbers have every line adding to 20? If it can, make one; if it can’t, say why not.
Hint
Answer
Use the temple square and the trick of its four biggest numbers to make a 4 × 4 magic square whose lines all add to 100. Then check one row and one diagonal.
Answer
Varahamihira’s perfume square (in the story above) uses each of 1 to 8 twice.
- a.Check that its rows, columns and diagonals all add to 18.
- b.Add 8 to the first and third numbers of its top row, the second and fourth of its second and third rows, and the first and third of its bottom row. Show that the result is a normal magic square of 1 to 16.
Answer
Dürer’s square has four corners adding to 34. Prove that the four corners of every normal 4 × 4 magic square add to 34, and so do its four middle boxes.
Hint
Answer
Where to go next
- Why Does 1089 Always Come Back? Another number pattern explained in one stroke by letters, the way the as and bs canceled in the 3 × 3 pattern.
- Is a Pattern That Works Ten Times Always True? Checking examples, and proving: the difference between trying a few squares and showing there is only one.
- Can You Solve the Puzzles of the Lilavati? More puzzles from India’s old problem books.
- How Do You Schedule a League So Everyone Plays Everyone? (Grades 9–12) Fair timetables built from grids where everything appears once in each row.
- How Is a Sudoku Solved in a Blink? (Computing, Going Further) How a computer fills in a Latin square with extra rules.
Where these facts come from
- The definitions (normal, order, magic constant ), no magic square of order 2, the Lo Shu legend and its dates (first clearly in the 1st-century Da Dai Liji, linked to the Luo river chart in the 12th century), Varahamihira’s Brihat Samhita (about 550 CE) and his perfume square with total 18, the Khajuraho square’s numbers, Narayana Pandita’s Ganita Kaumudi (1356) and the 384 pandiagonal squares, Frénicle’s 880 (published 1693), de la Loubère’s Du Royaume de Siam (1691), de la Loubère explaining the method with two preliminary squares, Dürer’s square in Melencolia I (1514) and talismans: Wikipedia, “Magic square”, read October 2026.
- The staircase method’s steps, de la Loubère’s embassy to Siam in 1687, and that he learned it from M. Vincent, who had learned it in Surat: Wikipedia, “Siamese method”, read October 2026.
- The Parshvanatha temple built by a Jain family between 950 and 970 CE, the most-perfect square at its entrance and its broken diagonals and 2 × 2 blocks: Wikipedia, “Parshvanatha temple, Khajuraho”, read October 2026. The square dated to the 12th century: “Magic square” above.
- The Khajuraho square as the first 4 × 4 magic square recorded, “most-perfect”, numbers two apart on a diagonal adding to 17, the 384 most-perfect squares all constructed by Narayana Pandita in 1356 with a knight’s-move method, the count proved exact by Rosser and Walker (Bull. Amer. Math. Soc. 44, 1938, as cited there): M. Bhargava, “The existence and uniqueness of magic-faced hypercubes, and applications to Khajuraho most-perfect magic squares, cubes, and hypercubes”, arXiv:2609.26762, September 2026, read October 2026.
- Manjul Bhargava’s Fields Medal (2014) and his post at Princeton: Wikipedia, “Manjul Bhargava”, read October 2026.
- Narayana Pandita’s dates and the Ganita Kaumudi, written in 1356, its final chapter on magic squares of every kind: J. J. O’Connor and E. F. Robertson, “Narayana”, MacTutor History of Mathematics, read October 2026.
- The counts of normal magic squares of orders 4, 5 and 6 and who found them (Frénicle, Schroeppel 1973, Mino 2024), order 7 not known: OEIS A006052, read October 2026.
- Sudoku solutions as Latin squares, and Choi Seok-jeong’s order-9 Latin squares of 1700, used to build a magic square: Wikipedia, “Latin square”, read October 2026.
- Latin squares in field trials, plots differing in fertility, and Ronald Fisher’s use of them in designing experiments: American Mathematical Society, Feature Column, “Latin Squares in Practice and in Theory I”, section 2, “Agronomy experiments”, read October 2026.
- Photos: Dürer’s square, a detail of Melencolia I (public domain), and the Khajuraho square by RainerTypke (CC BY-SA 3.0), both from Wikimedia Commons, looked at October 2026.
- Every number in this topic (all 362,880 orders of 1 to 9; all 7,040 4 × 4 squares, 384 of them most-perfect, every one with corners and middle making 34; the staircase squares of orders 3 to 9 and the failed 4 × 4; the turned diagonals and Dürer’s square; the temple square’s properties and its totals from 34 up; Varahamihira’s square raised to 1–16) computed in
checks/textbooks/math-further-6-8/09/tmp/magic_numbers.py, and again, independently, inchecks/textbooks/math-further-6-8/09/tmp/review/verify.pyandstair4.py.
