Magic square generator
Choose an order n from 3 to 21 and get an n × n square that uses 1 to n² once each, where every row, column and both diagonals add up to the same magic constant. Odd, doubly even and singly even orders are all supported.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Type the order n.
- Read the grid and the magic constant.
- Use the copy button to paste the grid into a spreadsheet.
How it works
For odd n the Siamese method puts 1 in the middle of the top row and moves up and to the right (wrapping around), dropping one row down when the cell is taken. The other two cases use the constructions named in the Method row.
Worked example
Order 3 gives 8 1 6 / 3 5 7 / 4 9 2. Each line sums to 3 × (9 + 1) / 2 = 15.
Tips
- Rotating or mirroring a magic square gives another valid one.
- For large orders the numbers in the picture get small; the copied text has them all.
Frequently asked questions
What is the magic constant?
The common sum of every row, column and main diagonal. For order n it is n(n² + 1) / 2: 15 for n = 3, 34 for n = 4 and 65 for n = 5.
Is the square unique?
No. Order 3 has one square up to rotation and reflection, but larger orders have many. This tool produces one standard construction for each order.
Why is there no square of order 2?
With the numbers 1 to 4 no arrangement makes all rows, columns and diagonals equal, so the smallest magic square is of order 3.
How are even orders built?
For orders divisible by 4, the numbers 1 to n² are written in rows and those on the diagonals of every 4 × 4 block are replaced by their complements n² + 1 − v. For orders 4k + 2, four odd squares are combined and some columns swapped (Strachey's method).