How to use the magic square solver
Pick the size, type the numbers you know and leave the rest blank. Find completions searches every possibility for 3 x 3 and 4 x 4 squares and tells you how many completions there are; if there is only one, it fills it in. If there are several, tap any of them to load it. The small gray figures beside the square are live sums: green when a full line adds up to the magic constant, red when it does not.
Check my square tells you whether a finished square is magic and names every row, column or diagonal that is off. Generate a magic square builds one for any size from 3 to 12 and explains the method used.
What makes a square magic
A normal magic square of size n uses each whole number from 1 to n² exactly once, and every row, every column and both main diagonals add up to the same total, the magic constant n(n² + 1) / 2. For a 3 x 3 square that is 15, for 4 x 4 it is 34, for 5 x 5 it is 65. The formula comes from adding 1 to n² and sharing the total equally between the n rows.
How many magic squares are there?
The solver can count them for you: leave a square completely blank and press Find completions. A 3 x 3 square has 8 completions, and they are all the same square turned or flipped (the Lo Shu square, with 5 in the middle). A 4 x 4 square has 7,040, which is 880 essentially different squares once rotations and reflections are counted as one. The solver lists them all; our tests check that every one of the 7,040 is magic and that none is repeated.
How the solver works
For completions the solver fills one empty cell at a time, always on the line with the fewest gaps. When a line has only one gap left, that number is forced (the magic constant minus the rest of the line), and when the numbers already in a line make the constant impossible, that branch is dropped at once. The search is exhaustive, so a count of 0 means no completion exists. It runs in the background, so the page stays responsive even for a blank 4 x 4.
Generated squares use three classic constructions, one per kind of size:
- Odd sizes (3, 5, 7...): the Siamese method. Start with 1 in the middle of the top row and keep moving one up and one right, wrapping around the edges; when the next cell is taken, drop down one instead.
- Sizes divisible by 4 (4, 8, 12): write the numbers in order, then in each 4 x 4 block replace the numbers on the block's diagonals by n² + 1 minus the number.
- Other even sizes (6, 10): John Conway's LUX method, which turns a Siamese square of half the size into 2 x 2 blocks.
Worked example
Put 2 in the top left corner and 8 in the bottom right of a 3 x 3 square, with 5 in the middle (press Example). The diagonal 2 + 5 + 8 = 15 already works. The solver finds exactly two completions, mirror images of each other: 2 7 6 / 9 5 1 / 4 3 8 and 2 9 4 / 7 5 3 / 6 1 8.
Tips for solving by hand
- In a 3 x 3 square the middle is always 5 and the corners are always even.
- Opposite cells through the center of a 3 x 3 square add up to 10.
- In a 4 x 4 square, start with a line that has three numbers: the fourth is forced.
Solver tested . Found a problem? Tell us.
Questions
What is the magic constant?
The total that every row, column and diagonal must reach: n(n² + 1) / 2 for a square of size n using 1 to n². It is 15 for 3 x 3, 34 for 4 x 4, 65 for 5 x 5 and 111 for 6 x 6.
Why does the middle of a 3 x 3 magic square have to be 5?
The middle row, the middle column and both diagonals all pass through the center. Adding those four lines gives 60, which counts every number once plus the center three extra times. All nine numbers add up to 45, so three times the center is 15 and the center is 5.
Can a magic square have repeated numbers?
Then it is not a normal magic square. The checker still tells you whether all the lines add up to the same total, and points out the repeated numbers.
Can the solver complete a 5 x 5 square?
Not yet. With only a few cells given, a 5 x 5 square has far too many completions to search and list in a browser. Use Generate for a 5 x 5 or larger square, and Check to test one you made.
Is there a 2 x 2 magic square?
No. With 1 to 4, the top row and the left column share a corner, so the other two numbers in them would have to be equal. A 1 x 1 square is magic only in a trivial sense.