How to use the magic square generator
- Enter the order of the square, a whole number from 3 to 16. A square of order 5 has 5 rows and 5 columns.
- Optionally open the advanced options to change the first number and the step between numbers. The classic square starts at 1 with a step of 1.
- Read the magic constant, the total that every row, column and diagonal has.
- Study the grid, and copy it or download it as a CSV file.
Formula
magic constant = n x (2 x first + (n^2 - 1) x step) / 2
for the classic square (first 1, step 1) the constant is n x (n^2 + 1) / 2
odd orders use the Siamese method; multiples of 4 use a complement pattern; other even orders use Strachey's method
The numbers are first + step x k for k from 0 to n^2 - 1, each used once. Every row, column and both main diagonals are added up and checked before the square is shown.
Squares of order 3, 4, 5 and 6
The order 3 square is 8 1 6 / 3 5 7 / 4 9 2 with a constant of 15. Order 4 has a constant of 34, order 5 has 65 (its first row is 17 24 1 8 15), order 6 has 111 and order 10 has 505. The formula gives 5 x 26 / 2 = 65 for order 5.
With a first number of 10 and a step of 2, the order 3 square uses 10, 12, 14 up to 26, and its constant is 3 x (20 + 8 x 2) / 2 = 54. The tests confirmed that every order from 3 to 40 gives a square whose rows, columns and diagonals all add to the constant, and that a square with two numbers swapped is rejected.
How the squares are built
For odd orders, the Siamese method places 1 in the top middle cell and moves up and to the right for each next number, dropping down a row when the cell is taken. For multiples of four, a pattern of diagonals marks the cells whose numbers are replaced by their complement. For orders that are even but not multiples of four, the order is split into four quarters, each a magic square of half the order, and then some cells are swapped.
Order 2 and other limits
No magic square of order 2 exists, so the tool starts at 3. It stops at 16 to keep the grid readable on a screen. Magic squares are not unique, and many other squares exist for each order; this tool gives one of them. For other puzzles, try the Sudoku Generator or the Pascal Triangle Generator.
More tools for playing with numbers
For more number patterns and conversions, try the Pythagorean Triples Generator, Collatz Sequence Calculator and Egyptian Fraction Decomposer.
Assumptions and limits
- The square is a normal magic square: every row, column and both main diagonals add to the same total.
- Each number appears exactly once.
- The order is a whole number from 3 to 16.
- The first number can be negative, and the step must be a whole number of at least 1.
- The order in the form is an example. Try your own.
Frequently asked questions
Is there only one magic square of each order?
No. For order 3 there is one square apart from turning and flipping it, but the number grows fast: order 4 has 880 and larger orders have far more. The tool gives one.
What is a magic constant?
It is the total that each row, column and main diagonal adds up to. For the numbers 1 to n squared it equals n times (n squared plus 1), divided by 2.
Why is there no square of order 2?
The four numbers 1 to 4 cannot be arranged so that both rows, both columns and both diagonals share a total, so no square of order 2 exists.
Can the squares use other numbers than 1 to n squared?
Yes, as long as the numbers are equally spaced. The first number and step in the advanced options give a square built from first + step times 0, 1, 2, and so on.