Advertisement
Advertisement
RECREATIONAL MATHS

Magic Square Generator — build one of any order, or test your own

Generate a normal magic square of any order from 3 to 12 using the method that fits its parity, or paste a grid and have every row, column and diagonal checked.

Generation picks the construction automatically from the order. Checking tests rows, columns, both diagonals and whether the entries are the numbers 1 to n squared.
Odd orders use the Siamese method, orders divisible by four use the criss-cross method, and orders of the form 4m+2 use Conway's LUX method. There is no magic square of order 2.
Magic constant
 
Order
Construction used
Sum of all entries
Verification
Square:
Working:
Tip: the bar panel shows each row and column sum against the magic constant. On a genuine magic square every bar is the same length, and that is a faster way to spot a broken square than reading fourteen numbers.
Advertisement

The magic square generator above builds a normal magic square of any order from 3 to 12 and verifies it before showing it to you. A normal magic square of order n contains each whole number from 1 to n² exactly once, arranged so that every row, every column and both main diagonals add to the same total. That total is the magic constant, and it is fixed by the order alone.

Arb Digital publishes it because the three construction methods are genuinely different depending on whether the order is odd, divisible by four, or of the form 4m+2, and because the order 2 case is impossible in a way worth explaining rather than glossing over. Our Pascal's triangle generator builds the other classic number array taught alongside this one.

What This Magic Square Generator Does

In generate mode it selects a construction from the parity of the order and runs it, then checks the result against every condition before displaying it: all n row sums, all n column sums, both diagonals, and the requirement that the entries are exactly the integers 1 to n² with no repeats and no gaps. The verification cell reports the outcome of that check rather than assuming the algorithm worked.

In check mode it takes a grid you paste in and runs the same tests. It reports which specific rows or columns fail rather than a bare yes or no, which is what you need when you are debugging a square by hand. It also distinguishes between a semi-magic square, where rows and columns work but a diagonal does not, and a square that is magic but not normal because its entries are not 1 to n².

Order 2 is handled explicitly. No magic square of order 2 exists, and the tool explains why rather than returning a broken grid. Order 1 is the trivial square containing just the number 1, which technically satisfies every condition and is usually excluded by convention.

How to Use It

  1. Pick generate or check from the first menu. The input below it changes to match.
  2. Set the order for generation. The tool names which of the three constructions it used, because the method is often the point of the exercise.
  3. Paste a grid to check it, one row per line. Entries may be separated by spaces, commas or tabs.
  4. Read the verification cell first. It is the only cell that tells you whether what follows is trustworthy.
  5. Scan the bar panel for uneven bars. Every row and column sum is drawn against the magic constant, so a failure is visible instantly.

The Magic Constant and Why It Is Forced

The numbers 1 to n² sum to n²(n²+1) ÷ 2. Those entries are distributed across n rows that all have to carry the same total, so each row must carry that grand total divided by n, which gives M = n(n²+1) ÷ 2. Wolfram MathWorld's page on the magic constant gives the same expression and lists the first values as 1, 5, 15, 34, 65, 111, 175 and 260.

Notice that the constant is completely determined before any arrangement is attempted. There is no freedom in it. A 3×3 normal magic square must have rows summing to 15, a 4×4 to 34, a 5×5 to 65. That is why the first thing to check about any candidate square is whether its row sums match the constant its order requires.

This also settles order 2 immediately. The constant would be 2 × 5 ÷ 2 = 5, so each row, each column and each diagonal of a 2×2 grid holding 1, 2, 3 and 4 would have to sum to 5. The two diagonal pairs and the two row pairs would all need to sum to 5, which forces the same number into two different cells. MathWorld notes the non-existence of the order 2 square directly. Our sum of squares calculator handles the related summation identities.

Advertisement

The Three Constructions

For odd orders the Siamese method works. Place 1 in the middle of the top row, then repeatedly move one square up and one to the right, wrapping around the edges. If the target cell is already occupied, drop one square below the last entry instead and carry on. MathWorld's magic square page records that the method is also called de la Loubère's method, after the French ambassador to Siam who reported it in Europe. Applied to order 3 it produces the square with rows 8 1 6, 3 5 7 and 4 9 2.

For orders divisible by four the criss-cross method is even simpler. Fill the grid with 1 to n² in reading order, then mark the cells lying on the diagonals of each 4×4 sub-block and replace each marked value v by n²+1−v. For order 4 that gives rows 16 2 3 13, 5 11 10 8, 9 7 6 12 and 4 14 15 1 — the arrangement that appears in Dürer's engraving Melencolia I, up to a swap of the two middle columns.

The remaining orders, those of the form 4m+2 such as 6 and 10, are the awkward case, and they get John Conway's LUX method. Build a magic square of order 2m+1 by the Siamese method, expand each cell into a 2×2 block, and fill each block with four consecutive numbers arranged in one of three patterns named L, U and X. The rows of blocks get m+1 rows of L, one of U and m−1 of X, and then one L in the centre swaps with the U below it. MathWorld describes the method under that name and notes it applies for m at least 1, which is why order 2 is outside it.

What Makes a Square Magic, Semi-Magic, or Neither

Three conditions have to hold together and they fail independently. Rows summing correctly is one; columns is another; the two main diagonals is a third. A square where rows and columns both work but a diagonal does not is called semi-magic, and it is far more common than a true magic square because the diagonals are the tightest constraint.

Separately there is the question of whether the square is normal. A magic square whose entries are not the consecutive integers 1 to n² can still have equal row, column and diagonal sums — take any magic square and add 10 to every entry, and it stays magic with a constant 10n larger. The tool reports normality separately from magicness because the two are genuinely different properties.

Counting is where the subject gets hard. Up to rotation and reflection there is exactly one magic square of order 3. There are 880 of order 4, and 275,305,224 of order 5. The number for order 6 is not known exactly and has only been estimated statistically. That explosion is why constructions matter: enumeration stopped being feasible three orders in.

Beyond the Basic Definition

Several stronger conditions have names. A pandiagonal or panmagic square requires every broken diagonal to sum correctly as well, which makes it magic under any cyclic shift of rows or columns. An associative square requires every pair of cells symmetric about the centre to sum to n²+1; the order 3 square has this property, and so do all Siamese-method squares.

A most-perfect magic square is both pandiagonal and has every 2×2 block of adjacent cells summing to twice the constant plus two; these exist only for orders divisible by four. A bimagic square stays magic when every entry is squared, which is a severe condition — the smallest known is order 8. None of these are produced by the constructions here, which target the plain definition.

The historical trail is long. The order 3 square appears in Chinese sources as the Lo Shu, in a story about markings on a turtle's shell, and reappears in Islamic, Indian and European mathematics over the following two millennia. Our digit sum calculator and arithmetic sequence calculator cover related number-pattern topics.

Need a generator that checks its own output?

Arb Digital builds free maths tools that verify the result before displaying it rather than trusting the algorithm.

Browse All Free Tools Talk To Our Team

Common Mistakes to Avoid

  • Expecting one method to cover every order — the Siamese method fails on even orders, and the criss-cross method fails on orders of the form 4m+2. Parity decides the construction.
  • Checking only the rows — rows are the easiest condition to satisfy by accident. The diagonals are where most candidate squares fall over.
  • Assuming an order 2 square exists — it does not, and the arithmetic forbidding it takes one line.
  • Confusing magic with normal — a square can have equal sums without containing 1 to n squared, and the two properties are checked separately here.
  • Counting rotations as different squares — the standard count treats a square and its seven rotations and reflections as one, which is why there is exactly one of order 3.

Related Free Tools From Arb Digital

Build the other classic array with the Pascal's triangle generator, work with grids of numbers using the matrix calculator, sum consecutive integers with the arithmetic sequence calculator, add digits with the digit sum calculator, or evaluate the summation identities with the sum of squares calculator. The full free online tools hub lists every mathematics tool we publish.

Frequently Asked Questions

What is the magic constant?

The total that every row, column and main diagonal must reach. For a normal square of order n it is n times n squared plus one, all divided by two, and it is fixed by the order before any arrangement is chosen.

Why is there no magic square of order 2?

The constant would be 5, so every row, column and diagonal of a grid holding 1, 2, 3 and 4 would have to sum to 5. Those conditions force the same value into two different cells, so no arrangement satisfies them.

Which construction is used for which order?

Odd orders use the Siamese method, orders divisible by four use the criss-cross method, and orders of the form four m plus two use Conway's LUX method. The tool picks automatically and names its choice.

What is a semi-magic square?

One where every row and column reaches the constant but at least one main diagonal does not. It is far more common than a true magic square because the diagonals are the hardest condition to satisfy.

Does a magic square have to contain 1 to n squared?

Only if it is normal. Adding the same amount to every entry preserves equal sums while breaking normality, so the two properties are checked separately here.

How many magic squares are there of each order?

Up to rotation and reflection there is one of order 3, 880 of order 4 and 275,305,224 of order 5. The count for order 6 is not known exactly and has only been estimated.

What is a pandiagonal magic square?

One where the broken diagonals also sum to the constant, which makes it stay magic under any cyclic shift of its rows or columns. The constructions used here do not generally produce them.

Is the order 3 square unique?

Yes, up to the eight rotations and reflections of the grid. Every 3 by 3 normal magic square is one of those eight views of the same arrangement, with 5 always in the centre.

This page covers a topic in recreational mathematics for educational purposes. The generated squares are verified against the full definition before display, but a construction that works for one order does not transfer to another parity.

Advertisement
Advertisement

Take it further