The Magic Codex

Magic Square Generator

Conjure a perfect magic square of order 3–9 — every row, column, and diagonal summing to the magic constant, verified line by line.

Odd orders use the Siamese method; 4 and 8 use doubly-even inversion. Order 6 is not supported — the tool will tell you why.

How to use the Magic Square Generator

  1. Pick an order from 3 to 9 — the square renders instantly.
  2. Read the magic constant beside the grid: that's the target sum every line hits.
  3. Scan the verification ticks — each row, column, and diagonal is re-added and checked for you.

Odd, doubly-even, and singly-even orders

Magic square construction splits into three families. Odd orders (3, 5, 7, 9) are built with the Siamese method: start with 1 in the top-middle cell, then step up-and-right, wrapping around the edges; when the cell is taken, drop one row down instead. Doubly-even orders (4, 8 — divisible by 4) are filled with 1..n² in reading order, then every value on a 4×4-sub-block diagonal is replaced with n²+1−v, rebalancing every line. Singly-even orders (6 here) need a third construction, the LUX method — this tool doesn't implement it yet, and says so rather than faking it.

Why the Lo Shu shows up at 3×3

There is essentially only one 3×3 normal magic square: every valid arrangement is a rotation or reflection of the ancient Lo Shu pattern. So the generator will always show you one of its eight orientations — try regenerating nothing; it's deterministic, and that's a feature, not a bug.

What is a magic square?
A magic square is an n×n grid filled with the numbers 1 to n² so that every row, column, and both main diagonals add up to the same total — the magic constant. The most famous is the 3×3 Lo Shu, whose lines all sum to 15; it has been known in China for over 4,000 years.
What is the magic constant?
For a normal magic square of order n, the magic constant is n(n²+1)/2. A 3×3 square sums to 15, a 4×4 to 34, a 5×5 to 65, an 8×8 to 260, and a 9×9 to 369. This tool displays the constant for every square it builds and checks each line against it.
How does the generator build odd-order squares?
It uses the Siamese (de la Loubère) method: place 1 in the top-middle cell, then repeatedly move one step up and one step right, wrapping around the edges; if the landing cell is already filled, drop one row down instead. It works for every odd order — 3, 5, 7, and 9.
How are 4×4 and 8×8 squares built?
Orders divisible by 4 are doubly-even and use a different construction: the grid is filled with 1..n² in reading order, then every number sitting on a diagonal of a 4×4 sub-block is replaced by n²+1−v. The swaps rebalance every row, column, and diagonal to the magic constant.
Why isn't order 6 supported?
Order 6 is singly-even (even but not divisible by 4): neither the odd-order Siamese method nor the doubly-even inversion applies, and it needs a third construction — the LUX method — which this tool doesn't implement yet. Orders 5 and 7 give you odd squares nearby; 4 and 8 give doubly-even ones.
How do I know the square is correct?
Every square the tool renders is verified line by line: the checker adds up all n rows, all n columns, and both diagonals, confirms each equals the magic constant, and confirms the numbers 1..n² each appear exactly once. The tick list under the square shows every check.
Is my input sent anywhere?
No. The square is computed in your browser with plain JavaScript. Nothing is uploaded, stored, or tracked.

Need help with this tool?

Found a bug, or have a suggestion for the codex? Write to us — a real human reads every message.

[email protected]

More from the codex