Magic Square Solver and Generator
Type in the numbers you already have and leave the rest empty.
Ramanujan's own birthday, 22 December 1887, is filled in to start.
How to solve a magic square with missing numbers
Start with the magic sum, the total every line has to reach. With the numbers 1 to 9 in a 3 by 3 it is 15, and in general it is the total of all the numbers divided by the number of rows, so 1 to 16 in a 4 by 4 gives 34. In a 3 by 3 using 1 to 9 the centre is always 5.
Then look for any row, column or diagonal with only one gap. That gap is not a guess: it is the magic sum minus the numbers already there. Fill it, and new lines with one gap appear. When none is left, try a number in the most constrained gap and see whether everything still adds up.
That is exactly how the solver works, which is why it finishes a worksheet instantly. Type in the numbers you have, leave the rest empty, and press Fill in the rest. It shows the answer and how many answers there are, so you can tell whether a puzzle has one solution or several.
Make a magic square of any size
The Make one tab builds a square of any size from 3 to 20 with the numbers 1 to n squared. Odd sizes use the Siamese method: start in the top middle, move up and right, and drop down a row when the way is blocked. Sizes divisible by four count straight through and swap each number on the diagonals of every 4 by 4 block for its partner. The awkward sizes, 6, 10, 14 and so on, are four smaller odd squares put together with a few columns exchanged, a method due to Ralph Strachey. Every square the page hands out is checked by actually adding up every line.
For a particular magic sum, enter it. Adding the same number to every cell keeps every line equal and the numbers all different, so a 3 by 3 can be made to add up to any multiple of 3, 12 included.
Ramanujan's birthday magic square
Srinivasa Ramanujan built a 4 by 4 square whose top row is his date of birth: 22, 12, 18, 87. Every row, column and diagonal adds up to 139, and so do the four corners, the four in the middle and each of the four 2 by 2 corners of the square. The same pattern works for any date, which is what the Birthday square tab does with yours.
How many magic squares are there?
With the numbers 1 to 9 there is exactly one 3 by 3 magic square, the Lo Shu, or eight if you count it turned and flipped. With 1 to 16 there are 880 of order four, or 7,040 counted that way. Both numbers come out of this page's own search, not out of a book. Order five has 275,305,224, counted by Richard Schroeppel in 1973, far too many to list here, which is why the solver stops counting at a point and says "at least".
More on magic squares
The Lo Shu, Durer's 1514 square and why there is only one 3 by 3 are in magic squares explained. If what you like is numbers that have to fit together exactly, Cube Alchemy is the same itch in solid form, and the rest of the range is in mechanical puzzles. There are more solvers on the puzzle tools page.
For sums written in letters, like SEND + MORE = MONEY, the cryptarithm solver finds the digits and shows the column logic step by step.
Frequently Asked Questions
How do you solve a magic square with missing numbers?
Work out the magic sum first: the total of all the numbers divided by the number of rows, so 15 for 1 to 9 in a 3 by 3. Then fill any row, column or diagonal with only one gap, since that number is forced, and repeat. This solver does exactly that and shows every answer.
What is the magic sum of a 3 by 3 magic square?
15, using the numbers 1 to 9. The centre is always 5. A 4 by 4 using 1 to 16 adds to 34, and a 5 by 5 using 1 to 25 adds to 65.
How many 4 by 4 magic squares are there?
880, using the numbers 1 to 16, or 7,040 if each one turned and flipped is counted separately. There is only one 3 by 3, the Lo Shu.
What is Ramanujan's birthday magic square?
A 4 by 4 square whose top row is Ramanujan's date of birth, 22, 12, 18 and 87. Every row, column and diagonal adds up to 139, and so do the corners, the centre four and each 2 by 2 corner. The same pattern works for any date.
Can a magic square add up to any number?
Not with whole numbers that are all different, by the simple method of shifting every cell by the same amount: a 3 by 3 can reach any multiple of 3, a 4 by 4 any sum 34 plus or minus a multiple of 4.