Math Calculators
Magic Square Generator - Odd & Even Order Magic Squares
Generate magic squares where every row, column, and diagonal sums to the same magic constant. Supports odd orders (3, 5, 7, 9, 11) and doubly-even orders (4, 8, 12).
Odd orders
Doubly-even orders
Magic constant: 15 - every row, column and diagonal sums to 15.
| 8 | 1 | 6 |
| 3 | 5 | 7 |
| 4 | 9 | 2 |
Construction Methods
Odd orders use the Siamese (de la Loubère) method: start in the top-middle cell and move up-right, wrapping around edges. When the next cell is occupied, move down one cell instead.
Doubly-even orders (multiples of 4) use a diagonal-reversal method: fill the grid with consecutive numbers, then flip values that lie on the diagonals of each 4×4 sub-block.
Properties of magic squares
In an n×n magic square using the numbers 1 through n²:
- Magic constant: every row, column, and main diagonal sums to M = n(n² + 1) / 2. For n = 3: M = 15. For n = 4: M = 34. For n = 5: M = 65.
- Sum of all cells: n²(n² + 1) / 2 (the sum of 1 through n²).
- Normal magic squares: use each of the integers 1…n² exactly once.
Historical note
Magic squares have fascinated mathematicians and mystics for thousands of years. The Lo Shu (3×3 square, magic constant 15) originates from ancient China; legend dates it to around 650 BCE when a turtle with a pattern on its shell emerged from the Lo River.
Albrecht Dürer embedded a 4×4 magic square (magic constant 34) in his 1514 engraving Melencolia I. The bottom row reads 16, 3, 2, 13 - the center cells of the bottom two rows encode the year “1514.”
Step-by-step: 3×3 Siamese method
Place numbers 1–9 in a 3×3 grid using the Siamese (de la Loubère) method. Start at the center of the top row and move up-right after each placement (wrapping around). If the destination is occupied, move down instead:
Step 1: place 1 at top-center
_ 1 _
_ _ _
_ _ _
Step 2: move up-right (wraps to bottom-right); place 2
_ 1 _
_ _ _
_ _ 2
Step 3: move up-right; place 3
_ 1 _
3 _ _
_ _ 2
Step 4: move up-right (cell occupied); move down instead; place 4
_ 1 _
3 _ _
4 _ 2
Continue ... final result:
2 7 6
9 5 1
4 3 8 (all rows, columns, diagonals sum to 15) Singly-even orders
Orders where n ≡ 2 (mod 4) - that is, n = 6, 10, 14, 18 … - are the hardest to construct. Neither the Siamese method (designed for odd orders) nor the diagonal-reversal method (designed for doubly-even orders like 4, 8, 12) applies directly.
The two main approaches for singly-even orders are the LUX method (Conway) and the Strachey method. Both involve constructing four odd-order sub-squares and carefully swapping elements between them to achieve the magic constant in every row, column, and diagonal.
Magic constant table
| Order (n) | Magic constant M = n(n²+1)/2 | Total sum |
|---|---|---|
| 3 | 15 | 45 |
| 4 | 34 | 136 |
| 5 | 65 | 325 |
| 6 | 111 | 666 |
| 7 | 175 | 1,225 |
| 8 | 260 | 2,080 |
| 9 | 369 | 3,321 |
| 10 | 505 | 5,050 |
Magic square variations
- Pandiagonal (panmagic) squares: all rows, columns, main diagonals, and broken diagonals (wrapping diagonals) sum to M. The smallest pandiagonal magic square is order 5.
- Associated (symmetric) squares: every pair of cells symmetrically opposite the center sums to n² + 1. In a 3×3 example, the center cell is 5 and every pair sums to 10.
- Most-perfect squares: a special class of doubly-even pandiagonal squares where every 2×2 sub-square also sums to the same constant.