I was watching an episode of Penn and Teller Fool Us with my parents, if you haven’t watched this show, you really should. It’s extremely entertaining, though my girlfriend gets very upset that the show doesn’t tell you exactly how the trick is done.

In this episode, a magician by the name of Amazing Allison does a trick involving a magic square – you can see itÂ here. When I saw it, I immediately wanted to know how it was done using mathematics.

A brief set-up: someone is asked for a number between 25 and 99, and then the performer creates a magic square, such that the rows and columns add up to the target number, and in addition a whole lot of other things add up to the target number, including the diagonals, each square of 4, etc. In my mind, there must be a formula for this, so I set out to find it.

Let us briefly consider a trivial solution: Suppose my target number is , then a trivial solution for an magic square is for all entries to equal . Of course, this is not a very interesting magic square, and quite easy to make! So let us improve this slightly by introducing a free variable – so now we have a target number , and we fill in one box with our free variable . The question is now, how will we find the remainder of our entries? Mathematically, this requires us to find linearly independent conditions upon the entries of the square, then we chuck it into a matrix and find the reduced row echelon form, and whaboom we’re done. Easy, right? I tried this using a magic square and it was surprisingly hard to find 15 linearly independent conditions! Here were all the conditions I added:

- All rows and columns must add to
- Each diagonal must add to
- The corners must add to
- Each square of 4 blocks must add to
- Each combination of non-corner exterior squares must add to
- Diagonal alternating patterns through the square from top-down must add to

and I was finally able to hit rank 15! The square yielded was as follows:

Now, this is a good start, but looking at it, it still isn’t very interesting!

Going forward there are a couple of conditions that we should impose on this, firstly that each entry should be positive and integer. This will admit a trivial solution only in the case where is divisible by 4. It may be better suited to a linear programming style problem, however, due to the integrality constraints. This will be further explored in part 2.