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:
2 | 7 | 6 |
9 | 5 | 1 |
4 | 3 | 8 |
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:
1 | 6 | 5 |
8 | 4 | 0 |
3 | 2 | 7 |
The history of magic squares
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:
- 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.)
- Create the centre column and centre row for the magic square.
- Calculate adjacent cells from the centre row and centre column.
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 (decimal) | Base 5 | ||
---|---|---|---|
0 | 0x100 | 0 | 0x50 |
1 | 1x100 | 1 | 1x50 |
2 | 2x100 | 2 | 2x50 |
3 | 3x100 | 3 | 3x50 |
4 | 4x100 | 4 | 4x50 |
5 | 5x100 | 10 | 1X51+0x50 |
6 | 6x100 | 11 | 1X51+1x50 |
7 | 7x100 | 12 | 1X51+2x50 |
8 | 8x100 | 13 | 1X51+3x50 |
9 | 9x100 | 14 | 1X51+4x50 |
10 | 1x101+0x100 | 20 | 2X51+0x50 |
… | … | … | … |
25 | 2x101+5x100 | 100 | 1x52+0x51+0x50 |
26 | 2x101+6x100 | 101 | 1x52+0x51+1x50 |
… | … | … | … |
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:
4,4 | ||||
3,3 | ||||
2,2 | ||||
1,1 | ||||
0,0 |
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:
24 | ||||
18 | ||||
12 | ||||
6 | ||||
0 |
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.
4,4 | ||||
3,3 | ||||
0, | 1, | 2,2 | 3, | 4, |
1,1 | ||||
0,0 |
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.
2,1 | 3,0 | 4,4 | 0,3 | 1,2 |
1,0 | 2,4 | 3,3 | 4,2 | 0,1 |
0,4 | 1,3 | 2,2 | 3,1 | 4,0 |
4,3 | 0,2 | 1,1 | 2,0 | 3,4 |
3,2 | 4,1 | 0,0 | 1,4 | 2,3 |
11 | 15 | 24 | 3 | 7 |
5 | 14 | 18 | 22 | 1 |
4 | 8 | 12 | 16 | 20 |
23 | 2 | 6 | 10 | 19 |
17 | 21 | 0 | 9 | 13 |
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 tuples | Base 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 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.