StatGardenREF. DESK
Calculators/Maths/Magic square
Maths

Magic square calculator

Finds the magic constant of an n×n magic square and builds it for odd n via the Siamese method.

Published 9 August 2026 · Updated 9 October 2026

What this calculator does

A magic square is an n by n grid holding the numbers 1 to n², arranged so that every row, every column and both diagonals add to the same total. That total is the magic constant, and it is fixed by the size of the square alone: M = n(n² + 1) ÷ 2. For a 3 by 3 square it is 15, and no arrangement of the numbers can produce any other value.

Building one is the harder part, and the method depends on whether the size is odd, singly even or doubly even. This calculator constructs odd-order squares directly using the Siamese method, and reports the magic constant for any size, including the sizes it does not draw.

The formula

FormulaMagic constant M = n(n²+1) / 2, filled with the integers 1 to n²

The numbers 1 to n² add to n²(n² + 1) ÷ 2, and that total is shared evenly among the n rows, which gives the magic constant n(n² + 1) ÷ 2. For odd n up to 11 the grid itself is filled by the Siamese method: start at the middle of the top row, then move one square up and one to the right for each next number, wrapping around the edges when you run off them, and drop one row straight down instead whenever the square you would land on is already taken.

TermMeaning
Magic constantThe shared total of every row, column and diagonal, written M.
OrderThe side length n of the square, which alone determines the magic constant.
Siamese methodA construction that fills any odd-order magic square by moving up and to the right, with a drop down whenever a square is occupied.
Doubly evenAn order divisible by 4, such as 4, 8 or 12, which has its own construction distinct from the odd and singly even cases.

The inputs explained

FieldWhat to enter
Square size (n, side length)The side length of the square. Any size gives a magic constant; odd sizes up to 11 are also drawn out in full.

When to use it

Checking a square you have built

Every row, column and diagonal of a finished square has to match the magic constant. Having that target first turns checking the square into a straightforward addition rather than a search for what the total ought to be.

Setting a puzzle with a known answer

A partly filled magic square makes a self-checking puzzle, since the missing entries are forced by the constant. The completed odd-order grids here give the answer to check against.

Exploring how the constant grows with size

The magic constant rises far faster than the side length, and comparing sizes directly is the clearest way to see that it depends on the cube of n rather than on n itself.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

What is the magic constant for each size of magic square?

Six square sizes, with the magic constant and the range of numbers each one uses.

Sizes 3 through 8
Size (n)Magic constant (row/column/diagonal sum)Numbers usedGrid size
3 × 3151 to 93 × 3
4 × 4341 to 164 × 4
5 × 5651 to 255 × 5
6 × 61111 to 366 × 6
7 × 71751 to 497 × 7
8 × 82601 to 648 × 8
The constant grows with the cube of n, not with n: 15 at size 3, 65 at size 5, and 260 at size 8. Size 2 is absent from the table because no 2 by 2 magic square exists. The constant would have to be 5, and while 1 with 4 and 2 with 3 both give it, no arrangement makes both diagonals work as well.

Questions

What is the magic constant of a 3 by 3 magic square?

15. The numbers 1 to 9 add to 45, and that total is shared evenly among three rows, so each row, column and diagonal must come to 15. Every valid 3 by 3 magic square has 5 in the centre, which follows from the same arithmetic.

Is there a 2 by 2 magic square?

No. The numbers 1 to 4 add to 10, so each row and column would need to total 5, which is achievable. Both diagonals would need 5 as well, and no arrangement of four distinct numbers satisfies all six conditions at once.

Why does the calculator only draw odd-sized squares?

Because the Siamese method it uses only works for odd orders. Even orders split into two further cases, doubly even and singly even, each needing a different construction. The magic constant is still reported for those sizes, since it depends only on n.

How many different 3 by 3 magic squares are there?

Just one, ignoring rotations and reflections. Counting those as distinct gives eight, since a square can be turned four ways and each of those mirrored. Larger orders open up very quickly: there are 880 essentially different 4 by 4 magic squares.

For the triangular numbers, another arrangement problem with a closed-form total, see the triangular number calculator. To sum an arithmetic run of numbers directly, see the sum of series calculator.