
Amaze your family and friends this holiday showing your mathematical prowess by generating instant magic squares at will. In the previous article we saw how to generate 4×4 magic squares. If that was a bit too hard, here is a simpler version for generating instant 3×3 magic squares. Learn the trick and some computer science about algorithms and how they prove they always work.
The Trick
First ask an audience member to pick a number out of a hat. That will be the target number. You then write out a magic square that adds to that number.
The Secret
Building this type of magic square is based on the algorithm below that creates magic squares from 9 consecutive numbers. The secret is first to make sure all the numbers you put in the hat are multiples of 3 (i.e. are in the 3 times table). You then follow the algorithm below that tells you what numbers to put where in the grid.
The Magical Algorithm
- Place lots of numbers on folded pieces of paper in a hat. All are multiples of 3 (but the audience do not know that).
- Ask an audience member to pull one out at random.
- Announce that that number is the TARGET number. You will create a magic square that adds up to that number so that is the number that the square rows and so on will add to.
- In your head divide that number by 3. For example, if TARGET was 15 THEN you divide 15 by 3 to get 5. Let’s call this value MID, to allow us to be general when we follow the rest of the instructions.
- On a 3 by 3 grid, put MID in the centre square (so in our example, put 5 in the middle).
- Place the number (MID + 3) in the upper right-hand square (in our example, 5+3 = 8).
- Place the number (MID – 3) in the lower left-hand square (in our example, 5-3 = 2).
- Place the number (MID + 1) in the upper left-hand square (in our example, 5+1 = 6).
- Place the number (MID – 1) in the lower right-hand square (in our example, 5-1 = 4).
- Fill in the remaining squares to make the magic square work, so that the rows and columns add to TARGET (subtracting the other two numbers from TARGET in each case to get the missing one).

For the last step, you just need to fill in the empty squares, to make sure the rows and columns add to the right number, TARGET. To do this you just need to keep in mind the target magic number you calculated. (For our example, remember it was 15). It’s a bit of simple arithmetic to find these final numbers and voila, you have built a magic square that adds up to a total picked at random..
Practice doing the maths in your head so that you can make it seem magical.
Does it always work?
You can actually prove the trick always works using some simple algebra based on the template magic square above. See if you can work out how yourself. Using MID and TARGET in place of numbers, for the trick to always generate a correct magic square you need to check that all rows and columns simplify to be equivalent to TARGET. Visit our Conjuring with Computation website to see the detail of how.
Proving a magic trick in this way is just the same thing computer scientists do when they invent new computing algorithms to make sure they work. It increases the assurance that the algorithm and so programs implementing it do work.
If you can program, then you could write a program to generate magic squares using the above algorithm, and then your proof would be a step in verifying your program, as long as it does correctly implement the algorithm!
More on …
- Conjuring with Computation: Instant Magic Squares (bonus chapter)
Buy the Book

Subscribe to be notified whenever we publish a new post to the CS4FN blog.
This page is funded by EPSRC on research agreement EP/W033615/1.
