Magic Square Generator

Create a grid in which every row, column and diagonal adds up to the same total, and see the check done for you.

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 nCellsSum of all numbersMagic constantCenter cellConstructionDistinct squares
3×3945155Odd: Siamese method (this tool)8 (1 up to rotation and reflection)
4×41613634none (no center cell)Doubly even (multiple of 4): separate method7,040 (880 up to rotation and reflection)
5×5253256513Odd: Siamese method (this tool)275,305,224 up to rotation and reflection
6×636666111none (no center cell)Singly even (4k + 2): separate methodvery large; still an active research topic
7×7491,22517525Odd: Siamese method (this tool)very large; still an active research topic
8×8642,080260none (no center cell)Doubly even (multiple of 4): separate methodvery large; still an active research topic
9×9813,32136941Odd: Siamese method (this tool)very large; still an active research topic
10×101005,050505none (no center cell)Singly even (4k + 2): separate methodvery 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

  1. Write 1 in the middle cell of the top row.
  2. 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.
  3. If the target cell is already filled, write the next number directly below the current cell instead.
  4. 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.

3×3 (constant 15)
816
357
492
5×5 (constant 65)
17241815
23571416
46132022
101219213
11182529
7×7 (constant 175)
3039481101928
384779182729
466817263537
5141625343645
1315243342444
2123324143312
2231404921120

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

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

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.

Related tools