Magic Squares, Constructed

After reading this you can compute the magic constant for any size, build an odd square by hand with the Siamese method, understand why the doubly even and singly even cases need different tricks, and check your own square for the magic property.

What a magic square is

A magic square of order n is an n by n grid that holds each whole number from 1 to n^2 exactly once, arranged so that every row, every column and both main diagonals add up to the same total. That total is called the magic constant.

The smallest interesting case is n = 3. There is essentially one 3 by 3 magic square, up to rotation and reflection:

The classic 3 by 3 square (the Lo Shu). Every line sums to 15.
Col 1Col 2Col 3Row sum
27615
95115
43815

Check a column: 2 + 9 + 4 = 15. Check the main diagonal: 2 + 5 + 8 = 15. The anti-diagonal: 6 + 5 + 4 = 15. All eight lines agree, so the square is magic.

The magic constant and why it is forced

The magic constant is not a free choice. Once you fix n, the numbers 1 to n^2 are fixed, so their total is fixed, and the row sum follows.

M = \frac{n(n^2 + 1)}{2}

Here M is the magic constant and n is the side length. The derivation is short. The numbers 1 through n^2 add up to \frac{n^2(n^2+1)}{2} by the standard sum formula. Those numbers fill n rows, and each row must carry the same share, so divide by n. The n in the denominator cancels one factor of n^2, leaving \frac{n(n^2+1)}{2}.

Plug in n = 3: \frac{3(9+1)}{2} = \frac{30}{2} = 15. For n = 4 you get \frac{4 \cdot 17}{2} = 34. For n = 5, \frac{5 \cdot 26}{2} = 65. The values grow like n^3/2, so a 10 by 10 square has magic constant 505.

M rises roughly as n cubed over two, so doubling the side multiplies the constant by about eight.

Three methods for three kinds of size

No single construction covers every size. The order splits into three families, and each family gets its own method.

Odd order
Sizes 3, 5, 7, 9, ... The Siamese (De la Loubère) method builds these by stepping diagonally.
Doubly even order
Sizes divisible by 4: 4, 8, 12, ... A complement trick keeps some cells in natural order and reflects the rest.
Singly even order
Sizes that are even but not divisible by 4: 6, 10, 14, ... The hardest case. The LUX method splits the square into odd blocks and patches them together.

The builder picks the right method for the size you request, but the logic is worth knowing because it explains why order 6 needs more work than order 5 or order 8.

The Siamese method, step by step

For odd n the rule is compact: place 1 in the middle of the top row, then always try to move up one row and right one column to place the next number. Two wrap rules handle the edges.

  • If moving up leaves the top, wrap to the bottom row of the same column.
  • If moving right leaves the grid, wrap to the leftmost column of the same row.
  • If the target cell is already filled (or you step off a corner), drop down one row from the current cell instead.

Wrapping treats the grid as a torus: the top edge connects to the bottom, and the right edge connects to the left. The diagonal walk then never truly leaves the board, it only reappears on the far side.

Building the 3 by 3 square from the defaults

The demo uses the default size, order 3, which produces exactly the Lo Shu square above. Follow the placements, using row 0 at the top and column 0 at the left.

  1. Place 1 in the middle of the top row: row 0, column 1.
  2. Up-right from (0,1) leaves the top and would wrap. The target becomes row 2, column 2. Place 2 there.
  3. Up-right from (2,2) leaves the right edge, so wrap the column to 0: target row 1, column 0. Place 3 there.
  4. Up-right from (1,0) gives row 0, column 1, which already holds 1. Blocked, so drop down one row: row 2, column 0. Place 4 there.
  5. Up-right from (2,0) gives row 1, column 1. Place 5 there.
  6. Up-right from (1,1) gives row 0, column 2. Place 6 there.
  7. Up-right from (0,2) wraps both ways to row 2, column 0, which holds 4. Blocked, drop down from (0,2) to row 1, column 2. Place 7 there.
  8. Up-right from (1,2) wraps to row 0, column 0. Place 8 there.
  9. Up-right from (0,0) wraps to row 2, column 1. Place 9 there.

The result reads [2 7 6 / 9 5 1 / 4 3 8], matching the table exactly. Every row, column and diagonal sums to 15.

A grid that fills itself using the Siamese method for the chosen odd order, one cell per step, showing the diagonal walk and the wrap and drop rules in action, with a live tally of each row, column and diagonal sum next to the grid.

The doubly even trick and the LUX method

For orders divisible by 4, start by writing 1 to n^2 in plain reading order, left to right and top to bottom. Then mark the cells on the two main diagonals of every 4 by 4 block. Leave the marked cells alone. Replace each unmarked value v with its complement n^2 + 1 - v. For n = 4, complements are taken against 17, so a 2 becomes 15, a 3 becomes 14, and the diagonal cells keep their natural values. The reflected and unreflected values balance each row and column to the constant 34.

Singly even orders (6, 10, 14) resist both tricks. The LUX method by J. H. Conway splits the n \times n grid into a \frac{n}{2} \times \frac{n}{2} arrangement of 2 by 2 blocks, labels the blocks L, U or X by a fixed pattern, and fills each block with four consecutive numbers in the order that its letter dictates. The letter shapes (the strokes of L, U and X) tell you which of the four cells gets the smallest number and in what direction the rest follow. It is fiddly by hand, which is exactly why order 6 has no clean one-line rule.

There is no 2 by 2 magic square. With only the numbers 1, 2, 3, 4 you cannot make both rows and both columns share a sum: the row totals would need to be (1+2+3+4)/2 = 5, but you can never place the four numbers so both diagonals also hit 5. Order 2 is genuinely impossible, so the smallest even square is order 4.

Reading and checking a finished square

To verify magic, you check 2n + 2 line sums: n rows, n columns, and 2 diagonals. For order 5 that is 12 checks, each expected to equal 65. Because the numbers 1 to n^2 appear once each, you also confirm no value repeats and none is missing.

A useful shortcut: sum only the rows first. If all n row sums equal M, the grand total is automatically correct, so you only need to check the columns and the two diagonals. A single wrong column then points straight to the pair of swapped cells.

Every one of the twelve lines reaches 65, the flat bar chart being the visual signature of a correct square.

Common mistakes

Three errors account for most broken squares.

  • Wrong wrap direction. In the Siamese method the walk goes up and right. Going down-right or up-left also produces a magic square, but mixing conventions mid-build breaks it. Pick one and hold it.
  • Forgetting the drop rule. When the up-right target is occupied, you drop from the current cell, not from the blocked target. Dropping from the wrong cell scrambles everything after it.
  • Applying Siamese to even orders. The diagonal walk only closes cleanly when n is odd. Feed it an even n and it collides with itself and fails. That collision is the whole reason the even cases need separate methods.

Related tools

Magic squares are a corner of recreational mathematics that connects to several other pattern generators here. For grids that evolve by local rules the way the Siamese walk moves cell to cell, see Conway's Game of Life and Langton's Ant, which both build large structure from a tiny rule. For number-theoretic patterns on a grid, try the Ulam Prime Spiral and Pascal's Triangle mod n. If you enjoy exact combinatorial puzzles with backtracking, the N-Queens Visualizer and the Fifteen Puzzle share the same flavor of constraint and parity. The Tower of Hanoi shows another closed-form count, 2^n - 1, next to the magic constant's \frac{n(n^2+1)}{2}.

Frequently asked questions

How many different magic squares are there for each size?

Order 3 has exactly one, ignoring the 8 rotations and reflections. Order 4 has 880 essentially different squares (7040 including symmetries). Order 5 jumps to 275,305,224. Order 6 is still unknown exactly; it is only estimated to be near 1.77 \times 10^{19}. The count explodes fast.

Why is there no order 2 magic square?

The required line sum would be (1+2+3+4)/2 = 5. To make both rows sum to 5 you need pairs like 1,4 and 2,3, but any such arrangement leaves at least one column or diagonal off 5. No placement of four distinct numbers satisfies all six lines, so order 2 cannot exist.

Does the demo always give the same 3 by 3 square?

Yes. The Siamese method is deterministic, so the default order 3 always yields [2 7 6 / 9 5 1 / 4 3 8]. Any other 3 by 3 magic square is a rotation or reflection of this one.

Can a magic square use numbers other than 1 to n squared?

The classic definition uses 1 to n^2. You can add a constant to every cell or scale them all, and the square stays magic with a shifted constant, but then it is no longer normal. This builder produces the normal form.

What is a doubly even versus singly even order?

Doubly even means divisible by 4 (4, 8, 12). Singly even means even but not divisible by 4 (6, 10, 14). The distinction matters because the fast complement trick works for doubly even sizes but fails for singly even ones, which is why the LUX method exists.