The maths of magic squares

Charles Kelly

A magic square is an arrangement of numbers in a square in which the sum of each row, column, and main diagonal is the same. The name for this shared total is the magic number. Traditionally magic squares contain the integers from 1 to n2, where n is the order of the magic square. This is a 3x3 magic square which uses the numbers 1 to 9:


In this article we will use the integers from 0 to n2 - 1 (you'll see why later on). But you can easily convert our magic squares to the traditional ones by adding one to each entry. So for this article, we would write the 3x3 magic square above as:


The history of magic squares

Lo Shu magic square

The Lo Shu magic square

Magic squares were known to Chinese mathematicians as early as the seventh century BCE. Chinese literature dating from 650 BCE tells the legend of Lo Shu, which means "scroll of the river Lo". In ancient China there was a huge flood. The great king Yu tried to channel the water out to sea when a turtle emerged from the water with a curious pattern on its shell: circular dots of numbers which were arranged in a three by three grid pattern such that the sum of the numbers in each row, column and diagonal was the same: 15. Fifteen is the number of days in each of the 24 cycles of the Chinese solar year.

Magic squares were also known to Islamic mathematicians, possibly as early as the 8th century, when Muslims came into contact with Indian culture, and learned Indian mathematics and astronomy. The Arab mathematician Ahmad al-Buni, who worked on magic squares around 1250 AD, attributed mystical properties to them. There are also references to the use of magic squares in astrological calculations, a practice that seems to have originated with the Arabs. (You can read more about the history of magic squares on Wikipedia.)

Three steps and two tricks

There are three steps to creating a magic square:

  1. Select the order (the number of cells in a row or column) and dimension (eg. 2 for two-dimensional square, 3 for three-dimensional cube and so on) for your magic square. (You've probably only ever seen two-dimensional magic squares, but magic cubes and their higher-dimensional cousins come in very handy, as you'll see in future articles.)
  2. Create the centre column and centre row for the magic square.
  3. Calculate adjacent cells from the centre row and centre column.
There are also two tricks you need to create a magic square using this method. The first is to use a different base for the digits in your square. The numbers we are used to using are written in base 10, they are called decimal numbers. If you have worked with low-level computer code you may be familiar with base 2, which is called binary, and base 16 which is called hexadecimal. Binary numbers use two digits {0, 1}, and hexadecimal has 16 digits {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}.

When we create magic squares we will use a numeric base that is equal to the order of the magic square. For example, if you are creating a 5x5 magic square (so it has dimension 2 and order 5), you will use base 5. (You can read more about using different number bases here.) When working in base 5, the numbers 0 to 4 are written in the familiar way: 0, 1, 2, 3 and 4. But numbers from 5 and onwards are written in a less familiar way in base 5.

Base 10
Base 5

The second trick is to think of the numbers in each cell of the square as a group of digits, rather than as a single integer. This group of digits is called a tuple, and the number of digits in your tuples is given by the dimension of the magic square. So if you are creating a two-dimensional magic square your tuples will contain 2 digits. (A three-dimensional magic cube would have tuples with 3 digits, a four-dimensional magic hyper-cube would have tuples continuing 4 digits. This algorithm allows you to create magic squares of as many dimensions as you want.)

Really each digit in the tuple is a digit in numbers expressed in the numeric base you are using. So continuing our example of a 5x5 magic square, where you write numbers in base 5, each of your tuples will contain two of the symbols from the set {0,1,2,3,4} and together these form the two digits of a base 5 number. The smallest number you can express in base 5 is 0, which is equivalent to the tuple (0,0). The largest number you can express with two digits in base 5 is 44 written with the tuple (4,4) – this is equivalent to the decimal number 4x51+4x50=24. So using two-digit base 5 numbers (written as tuples for the purpose of creating a magic square) gives us 25 different numbers (0 to 24=52-1) to fill the 25 cells in our magic square.

We'll illustrate this by using our algorithm to work through this example.

Creating a magic square

We begin by creating the centre column of tuples for the magic square. The two symbols in each tuple are the same in each cell in the centre column, arranged in ascending order. So for a 5x5 magic square we get:


The tuples in our magic square are really numbers written in base 5: so (4,4) becomes 4x51+4x50=24 in decimal numbers. So, written in decimal numbers, our magic square so far is:


To fill in the rest of the cells for your magic square, you need to work with each digit within a tuple separately. As you move from one cell to the adjacent cell to its right, the leftmost symbol in the tuple will (usually) increase. Let's start with the central cell in our 5x5 magic square. This contains the tuple (2,2). The cell immediately to the right will have a tuple starting with 3 and the cell immediately to the right again will have a tuple starting with 4. We have now run out of cells to the right, so we simply wrap around to the left-most cell in the same row. Now 4 is the largest single digit number in base 5. Rather than counting on upwards and going into double digits, we switch back to zero in the next cell along to the right. So in our 5x5 magic square this occurs as you wrap-around from the rightmost cell in the third row, to the leftmost cell in the third row.


Fill in the first digits of the other tuples using the same rules. For the second digits in the tuples, do the same thing, only moving from right to left rather than left to right. As you move from one cell to the adjacent cell on its left, the rightmost digit in the tuple will increase in the same way.

After you have filled in the digits within a tuple, you can calculate its decimal equivalent.


As you would hope for a magic square, the sum of the numbers in each row, column, and principal diagonal is 60: the magic number. There is a simple rule for expressing the magic number for two dimensional magic square in base n. First, notice that all of the base 5 numbers appear exactly once within each tuple position within each row and column. This indicates that the sum of these number in each tuple position is equal to the sum of the numbers from zero to n-1. This is equal to n(n-1)/2. Then since each tuple position really represents a digit in number written in base n, the magic number must be

  \[  \frac{n(n-1)}{2} \times n^1 + \frac{n(n-1)}{2} \times n^0 = \frac{n(n-1)}{2} \times (n+1).  \]    

So for our 5x5 magic square above, n=5 and so the magic number equals 60 .

Symmetry and magic squares

Now we have completed our 5x5 magic square! You can check it works by adding together the numbers in any row, column or diagonal: they all add to the magic number, 60.

But looking back to the base 5 tuple version of the magic square, it illustrates the symmetry that is lost when you convert the tuples into decimal numbers. Notice the value of the leftmost value in each tuple on the northwest to southeast diagonal (in red) and the rightmost value in each tuple on the northeast to southwest diagonal (in green).

Base 5 tuplesBase 10 integers

Cutting a torus along the pink and red lines will open it up into a rectangle, which is topologically the same as a square. Image from Wikipedia.

You’ll notice that these diagonals wrap-around the magic square. An alternative way to picture how a two-dimensional square that wraps-around is a torus. A torus is the mathematical name for a doughnut and you can create one by gluing together the opposite edges of a piece of paper together. If you wrote your magic square on a torus, there is no need to wrap-around – the rows, columns, and diagonals would be continuous.

This is not the only way to create magic squares, you can see some other methods in this article. In future articles we will see how to create a two-dimensional Sudoku puzzle from a four-dimensional magic square. We will also discuss how the algorithms for creating magic squares and Sudoku puzzles can be applied to fields such as proteomics and quantum computing. Magic squares and sudoku have proved very useful, as well as entertaining!

About the author

Charles Kelly

Charles Kelly lives on a llama ranch in Skagit County, Washington. He enjoys teaching/practicing yoga and recreational mathematics. He also enjoys researching science, technology, economics, and politics.

During his academic career, he taught finance, accounting, production, and statistics at the University of San Diego. During his technical career, he designed and built systems for encryption, media, telecommunications, and logistics.

Currently, he employs his interests in robotics to design communities that are planned around driverless vehicles.


Check out the Magic Square on the wall in the background of Albrecht Durer's famous print Melanchoia

Haven't seen this idea before but obvious once you make the inspired change of base to represent the numbers.

It doesn't work for even ordered magic squares (e.g. 4x4). Is there a similar technique to apply for even ordered magic squares?

I haven't yet found a method for working with even order magic squares.

Thanks for your comment.


Very good article.

Thank you.


Thank you for this interesting article that mentions the torus construction. You can find more information about magic tori on the pages listed here:
The findings have resulted in a new OEIS sequence:
"Number of magic tori of order n composed of the numbers from 1 to n^2."
which is to be compared with the previous OEIS sequence:
"Number of magic squares of order n composed of the numbers from 1 to n^2, counted up to rotations and reflections."

Is there a formula for 3x3 squares that have 2-digit numbers as well?