What is a magic square?
A magic square of order n places the integers 1 through n² in an n × n grid, each exactly once, so that every row, every column and both main diagonals have the same sum. That shared sum is called the magic constant. This generator builds odd-order squares from 3 × 3 up to 15 × 15 with the Siamese method (also known as the De la Loubère method), then immediately checks all 2n + 2 lines against the constant, so you see the proof as well as the picture.
Magic squares are handy for puzzle setters who need a filled grid to blank out, for teachers who want a concrete example of arithmetic series and pattern-finding, and for anyone who simply likes number curiosities.
Where the magic constant comes from
The numbers 1 to n² add up to n²(n² + 1) ÷ 2. All of them are spread across n rows with equal sums, so each row must total that amount divided by n:
magic constant = n × (n² + 1) ÷ 2
For n = 3 the numbers 1 to 9 total 45, so each line is 45 ÷ 3 = 15. For n = 5 the total is 325, so each line is 65. In any odd-order magic square built by this method the center cell is (n² + 1) ÷ 2, which is exactly the constant divided by n. A line through the center therefore contains n values averaging the center value.
Reference table by order
The table is computed from the formulas when the page is built. The count column is the number of distinct squares for that order, which is well documented only for the smallest sizes.
| Order n | Cells | Sum of all numbers | Magic constant | Center cell | Construction | Distinct squares |
|---|---|---|---|---|---|---|
| 3×3 | 9 | 45 | 15 | 5 | Odd: Siamese method (this tool) | 8 (1 up to rotation and reflection) |
| 4×4 | 16 | 136 | 34 | none (no center cell) | Doubly even (multiple of 4): separate method | 7,040 (880 up to rotation and reflection) |
| 5×5 | 25 | 325 | 65 | 13 | Odd: Siamese method (this tool) | 275,305,224 up to rotation and reflection |
| 6×6 | 36 | 666 | 111 | none (no center cell) | Singly even (4k + 2): separate method | very large; still an active research topic |
| 7×7 | 49 | 1,225 | 175 | 25 | Odd: Siamese method (this tool) | very large; still an active research topic |
| 8×8 | 64 | 2,080 | 260 | none (no center cell) | Doubly even (multiple of 4): separate method | very large; still an active research topic |
| 9×9 | 81 | 3,321 | 369 | 41 | Odd: Siamese method (this tool) | very large; still an active research topic |
| 10×10 | 100 | 5,050 | 505 | none (no center cell) | Singly even (4k + 2): separate method | very large; still an active research topic |
There is no 2 × 2 magic square. The 3 × 3 case is unique up to rotations and reflections: the eight versions are all rotations or mirror images of one pattern. For 6 × 6 and above, exact counts are not simple to state, so none are listed.
How the Siamese method works
- Write 1 in the middle cell of the top row.
- Move one step up and one step right to place the next number. If you go off the grid, wrap around to the opposite edge: off the top lands you on the bottom row, off the right lands you in the leftmost column.
- If the target cell is already filled, write the next number directly below the current cell instead.
- Repeat until n² has been placed.
These are the 3 × 3 and 5 × 5 squares the rule produces, calculated by the same algorithm at build time.
| 8 | 1 | 6 |
| 3 | 5 | 7 |
| 4 | 9 | 2 |
| 17 | 24 | 1 | 8 | 15 |
| 23 | 5 | 7 | 14 | 16 |
| 4 | 6 | 13 | 20 | 22 |
| 10 | 12 | 19 | 21 | 3 |
| 11 | 18 | 25 | 2 | 9 |
| 30 | 39 | 48 | 1 | 10 | 19 | 28 |
| 38 | 47 | 7 | 9 | 18 | 27 | 29 |
| 46 | 6 | 8 | 17 | 26 | 35 | 37 |
| 5 | 14 | 16 | 25 | 34 | 36 | 45 |
| 13 | 15 | 24 | 33 | 42 | 44 | 4 |
| 21 | 23 | 32 | 41 | 43 | 3 | 12 |
| 22 | 31 | 40 | 49 | 2 | 11 | 20 |
The 3 × 3 result is a mirror image of the ancient Lo Shu square, which is one of the eight equivalent forms. One more property worth knowing: in these Siamese squares, any two cells that are mirrored through the center add up to n² + 1. In the 5 × 5 square above, the corner 17 faces the opposite corner 9, and 17 + 9 = 26 = 25 + 1. Squares with this property are called associative.
Worked examples (checked with code)
Example 1: the 3 × 3 square. First row 8 + 1 + 6 = 15, first column 8 + 3 + 4 = 15, main diagonal 15, anti-diagonal 15. All equal 15.
Example 2: the 5 × 5 square. Middle row 4 + 6 + 13 + 20 + 22 = 65, middle column 65, diagonals 65 and 65. All equal 65, and the center cell is 13 = (25 + 1) ÷ 2.
Example 3: Dürer's 4 × 4. The square in Albrecht Dürer's 1514 engraving "Melencolia I" starts with the row 16, 3, 2, 13. Its constant is 34. Row sums are 34, 34, 34, 34; column sums 34, 34, 34, 34; diagonals 34 and 34. The four corners also add to 34, as does each 2 × 2 quadrant. The two adjacent cells 15 and 14 in the bottom row are often read as the year 1514. This is an even-order square, so it is shown only as an example and not produced by this tool.
Transformations that keep a square magic
- Rotate or reflect it. A square gives eight versions, four rotations each with a mirror image.
- Add the same number to every cell. The constant increases by n times that number (the cells then no longer run from 1 to n²).
- Multiply every cell by the same number. The constant is multiplied by it too.
- Replace each x with n² + 1 − x. The result is again magic, with the same constant.
These operations give you plenty of variants from one generated grid, which is useful when you need several different puzzles without rebuilding anything.
Common misunderstandings and limits
- Each size produces one fixed square. The method is deterministic. Many other magic squares of the same size exist, but this tool shows this one; use the transformations above for variety.
- Only odd orders. Even orders (4, 6, 8, …) need other constructions, which differ depending on whether n is a multiple of 4.
- It is not a "pandiagonal" square. The check covers rows, columns and the two main diagonals only. The broken diagonals of these Siamese squares do not all share the same sum.
- The constant refers to lines only. Other groups, such as the four corners or small blocks, do not have to add up to a particular value in a general magic square.
- The number range is fixed at 1 to n². To use other numbers, apply the add or multiply transformations by hand.
Using the result
In class, ask students to predict the constant from the formula first, then compare it to the generated square and to the check shown in the result box. For puzzles, copy the grid with the button, blank out several cells, and let solvers use the facts that the center is (n² + 1) ÷ 2 and that every line must add up to the constant. The copy button places a tab-separated table in your clipboard, so it pastes cleanly into a spreadsheet or document.
Frequently Asked Questions
Can I make an even-sized magic square?
Yes, even-order magic squares exist for every even size from 4 up. The only exception is 2 × 2, which does not exist. They require different methods for sizes that are multiples of 4 and for sizes of the form 4k + 2, so this tool covers only odd orders. Dürer's 4 × 4 is shown above as an example.
How can I tell the generated square is correct?
The result box runs the check automatically: it sums every row, every column and both diagonals and compares them with the magic constant. You can also add a few lines by hand.
Why is there no 2 × 2 magic square?
Label the cells a, b over c, d. The first row and the first column both contain a, so a + b = a + c forces b = c. A magic square needs different numbers in every cell, so this is impossible.
How many 3 × 3 magic squares are there?
Eight if rotations and reflections count separately, and one if they are treated as the same. In every case the center is 5 and the corners hold even numbers. A brute-force search over all arrangements of 1 to 9 confirms exactly eight.
Where do magic squares come from?
The 3 × 3 square appears in ancient Chinese tradition as the Lo Shu, and Dürer's 4 × 4 is a well-known European example. Magic squares have been studied as a puzzle and as a symbol for centuries on several continents.