Magic square calculator
Build a magic square of any order, or paste a grid to check its rows, columns and diagonals.
One row per line, numbers separated by spaces or commas. The grid must be square.
Magic squares have been a favorite of puzzle makers, mathematicians and artists for well over two thousand years. This page builds a normal magic square of any order from 3 to 30 and tells you exactly which method was used. It also checks a grid you type in and explains what is wrong when the sums do not match, which is handy for homework, puzzle books and classroom exercises.
What is a magic square?
A magic square is a grid of numbers in which every row, every column and both long diagonals add up to the same total, called the magic constant. A normal magic square of order n uses each whole number from 1 to n² exactly once. The smallest one is 3 × 3: 8 1 6 / 3 5 7 / 4 9 2. Every row, column and diagonal of it adds up to 15.
How do I calculate the magic constant?
For a normal square of order n the numbers 1 to n² add up to n²(n² + 1) ÷ 2, and this total is shared equally by n rows, so each row adds up to n(n² + 1) ÷ 2. The constants for orders 3: 15, 4: 34, 5: 65, 6: 111, 7: 175, 8: 260. For order 4, for example, 4 × (16 + 1) ÷ 2 = 34.
How do I make an odd-order magic square?
Use the Siamese method, described for Europeans by Simon de la Loubère in 1691. Put 1 in the middle of the top row. Then keep moving one step up and one step right, wrapping around the edges, and write the next number there. If that cell is already taken, drop one cell down from the last number instead. The calculator builds all odd orders this way; for order 3 you get 8 1 6 / 3 5 7 / 4 9 2.
How do I make a magic square of even order?
Orders divisible by 4 are built by writing 1 to n² row by row and then replacing the numbers on the diagonals of each 4 × 4 block by n² + 1 minus the number. Order 4 gives 16 2 3 13 / 5 11 10 8 / 9 7 6 12 / 4 14 15 1 with constant 34. Orders such as 6, 10 and 14 (a multiple of 4 plus 2) use Strachey's method: four odd-order squares of half the size are placed in the corners with the number blocks 0, 2, 3 and 1 times (n/2)², and then some columns are swapped between the left and right quarters. Order 6 gives 35 1 6 26 19 24 / 3 32 7 21 23 25 / 31 9 2 22 27 20 / 8 28 33 17 10 15 / 30 5 34 12 14 16 / 4 36 29 13 18 11, constant 111.
Is there a magic square of order 2?
No, not with the numbers 1 to 4 (our derivation). Write the square as a, b over c, d. The first row and the first column both contain a, so a + b = a + c, which forces b = c, but all four numbers must be different. That is why the calculator starts at order 3.
What is the Lo Shu square?
The Lo Shu is the 3 × 3 magic square of the numbers 1 to 9 in Chinese legend, associated with the markings on a turtle shell. Wikipedia reports that the third-order square was known to Chinese mathematicians as early as 190 BCE and that stories about the Lo Shu go back to about 650 BCE. There is essentially only one normal 3 × 3 square; the other seven are its rotations and mirror images.
What is the magic square in Dürer's Melencolia I?
Albrecht Dürer's 1514 engraving Melencolia I contains a 4 × 4 square, 16 3 2 13 / 5 10 11 8 / 9 6 7 12 / 4 15 14 1. Its constant is 34, rows, columns and diagonals all give it, and the two middle cells of the bottom row, 15 and 14, give the year of the engraving, 1514. You can paste it into the “Check a square” tab to see the sums.
How many different magic squares are there?
Counting rotations and reflections as the same square, there is one of order 3, 880 of order 4 (a count credited to Frenicle) and 275,305,224 of order 5, according to the Wikipedia article on magic squares. The numbers grow explosively after that. The calculator gives you one square for each order, not all of them.
How the three construction methods work
Odd order (Siamese method). Start at the top middle with 1 and always step up and to the right, wrapping around, writing the numbers 1, 2, 3, … in order. When the next cell is occupied, move down one cell from the last number instead. The result is magic for every odd n.
Order 4k. Write the numbers 1 to n² row by row. In every 4 × 4 block, the cells on its two diagonals are replaced by the complement n² + 1 − x. Since complements pair up numbers that add up to n² + 1, every row, column and diagonal gets the right total.
Order 4k + 2 (Strachey). Let m = n ÷ 2, which is odd, and k = (m − 1) ÷ 2. Build a Siamese square of order m and use it four times, adding 0, 2m², 3m² and m² to its entries for the top-left, top-right, bottom-left and bottom-right quarters. Then swap the leftmost k columns of the top-left quarter with those of the bottom-left (with a one-cell shift in the middle row), and swap the rightmost k − 1 columns of the top-right quarter with those of the bottom-right. The method is credited to Strachey in W. W. Rouse Ball's Mathematical Recreations and Essays (1911), via Wikipedia. Another well-known procedure for this order is Conway's LUX method; the squares differ, but both are magic.
Worked example
Check the square from Dürer's engraving, 16 3 2 13 / 5 10 11 8 / 9 6 7 12 / 4 15 14 1. The rows add up to 34, 34, 34, 34; the columns to 34, 34, 34, 34; the diagonals to 34 and 34. All are 34, and the constant for order 4 is 4 × 17 ÷ 2 = 34. The 16 numbers are 1 to 16 once each, so the grid is a normal magic square. Interchange two numbers and the checker names the rows and columns that now disagree.
Common mistakes and limits
- Forgetting the diagonals. A grid whose rows and columns agree but whose diagonals do not is called semi-magic. The checker reports that case separately and shows both diagonal sums.
- Mixing up the “not 1 to n²” case. A grid like 2 7 6 / 9 5 1 / 4 3 8 plus 10 added to every cell still has equal sums, but its constant is not n(n² + 1) ÷ 2 and it does not use 1 to n². The page labels it “Magic square (not 1 to n²)”.
- Expecting every magic square to come out of the formula. The methods build one square per order. There are many others (880 of order 4), so your textbook or puzzle may show a different, equally valid square.
- Thinking Dürer's square is the only one for 34. Any magic square of order 4 on 1 to 16 has constant 34; Dürer's is one of the 880.
- Large orders. The page stops at order 30 so that the table stays readable; the methods themselves work for any order above 2.
Sources: Wikipedia, “Magic square” (constant formula, Lo Shu dating, Dürer's 1514 square, counts of 880 and 275,305,224, de la Loubère 1691) and “Strachey method for magic squares” (itself citing W. W. Rouse Ball, Mathematical Recreations and Essays, 1911; the book was not opened). Every square the page builds is verified by the same row, column and diagonal check you can use above. Related: sequence calculator, Pascal's triangle, Fibonacci calculator, factorial calculator and matrix calculator.