Magic square generator and guide
Choose an order from 3 to 15 to build a magic square and see its magic constant. Every row, column and diagonal is added up so you can check it.
Magic constant
per row, column and diagonal
How this is calculated
A magic square is an n × n grid of the distinct numbers 1 to n² in which every row, every column and both main diagonals add up to the same sum, called the magic constant. For order n the constant is n(n² + 1) / 2, so every 3 × 3 magic square adds up to 15.
The magic constant is the total of 1 to n², shared equally between n rows:
M = n(n² + 1) / 2
For n = 5 that is 5 × 26 / 2 = 65. For n = 3 it is 3 × 10 / 2 = 15. The constant is always a whole number for the orders this page builds. The magic constant formula guide has the proof.
The grid is built by one of three standard methods, chosen by the order. The same order always gives the same square, so the result can be repeated.
- Odd n (3, 5, 7 …): the Siamese method. Put 1 in the middle of the top row, then move one cell up and one to the right (wrapping around the edges) for each next number. If that cell is already taken, drop one cell down from the number just placed instead.
- n divisible by 4 (4, 8, 12): write 1 to n² in order, row by row. In every 4 × 4 block, replace each number v on the two diagonals with n² + 1 − v.
- n = 6, 10, 14: Conway’s LUX method. Build the Siamese square of order n/2 (3, 5 or 7), label its rows L, U and X, swap one pair of labels, then replace each cell with a 2 × 2 block of four consecutive numbers laid out by its label.
The generator supports orders 3 to 15 and builds normal magic squares only: the numbers 1 to n², each used once. After building a square, the page adds up all 2n + 2 lines (n rows, n columns and 2 diagonals) and lists each one so you can check the result by eye.
Many other magic squares exist for most orders. This page shows one square per order, not a catalogue. To fill in a grid you have started yourself, use the magic square solver.
Sources
- Weisstein, Eric W. “Magic Square.” MathWorld, A Wolfram Resource. mathworld.wolfram.com/MagicSquare.html (constant formula, Siamese and LUX methods).
- “Magic square.” Wikipedia. en.wikipedia.org/wiki/Magic_square (methods for odd, doubly even and singly even orders; no order-2 square).
A worked 3 × 3 example
| Column 1 | Column 2 | Column 3 | Diagonal ↗ sum ↗ 15 | |
|---|---|---|---|---|
| Row 1 | 8 | 1 | 6 | Row 1 sum 15 |
| Row 2 | 3 | 5 | 7 | Row 2 sum 15 |
| Row 3 | 4 | 9 | 2 | Row 3 sum 15 |
| Column sums | Column 1 sum 15 | Column 2 sum 15 | Column 3 sum 15 | Diagonal ↘ sum ↘ 15 |
The grey figures are the sums of each row (right), each column (bottom) and the two diagonals (corners). Every one is 15.
Check it by hand: rows 8 + 1 + 6, 3 + 5 + 7 and 4 + 9 + 2 each make 15; columns 8 + 3 + 4, 1 + 5 + 9 and 6 + 7 + 2 each make 15; diagonals 8 + 5 + 2 and 6 + 5 + 4 each make 15. This is the square the Siamese method gives for n = 3. For why its centre must be 5, see the 3 × 3 magic square guide.
Why there is no 2 × 2 magic square
With the numbers 1 to 4 the constant would be 2 × 5 / 2 = 5, so a + b = 5 and a + c = 5 for the top row and left column. That forces b = c, so a number would have to repeat, which a normal magic square forbids. The same argument rules out every arrangement of 1 to 4.
Where to go next
Tools
- Magic square solver Complete a partly filled 3x3 or 4x4 grid and see every solution.
- Magic square puzzle Fill a 3x3 or 4x4 grid with live line checks and hints.
- Magic square worksheets Make printable 3x3 or 4x4 puzzle sheets with an answer page.
Guides
- 3x3 magic square: the solution and why it works The one 3x3 magic square, why its sum is 15 and why 5 is in the centre, with a viewer for its eight forms.
- 4x4 magic square The sum, the 880 distinct squares, a hand method to build one and a routine to finish a partly filled grid.
- Magic constant formula: n(n²+1)/2 explained Every row, column and diagonal of a normal n x n magic square adds to n(n²+1)/2. A short proof, a table for 3x3 to 12x12 and a hand check.
- How to make a magic square: odd and even methods Three hand methods that build a magic square of any size from 3 up: Siamese for odd orders, a complement trick for multiples of 4 and Conway's LUX for the rest.
- Dürer's magic square from Melencolia I The 4x4 magic square in Dürer's 1514 engraving Melencolia I: its sum of 34, the date in the bottom row, and 20 of the many patterns that add to 34, which you can highlight.
- 5x5 magic square The sum of 65, an eight-stage Siamese walk-through you can step through, a check of the finished square and why 5x5 squares can't be listed.
- History of magic squares: from the Lo Shu to today A short timeline of magic squares, from the Lo Shu legend to Dürer, Agrippa, the 880 squares of order four and the unsolved square of squares, each entry with a source.
To build a square by hand, see how to make a magic square.