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 n^{2}, 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 n^{2}  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 alBuni, 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 twodimensional square, 3 for threedimensional cube and so on) for your magic square. (You've probably only ever seen twodimensional magic squares, but magic cubes and their higherdimensional 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  0x10^{0}  0  0x5^{0} 
1  1x10^{0}  1  1x5^{0} 
2  2x10^{0}  2  2x5^{0} 
3  3x10^{0}  3  3x5^{0} 
4  4x10^{0}  4  4x5^{0} 
5  5x10^{0}  10  1X5^{1}+0x5^{0} 
6  6x10^{0}  11  1X5^{1}+1x5^{0} 
7  7x10^{0}  12  1X5^{1}+2x5^{0} 
8  8x10^{0}  13  1X5^{1}+3x5^{0} 
9  9x10^{0}  14  1X5^{1}+4x5^{0} 
10  1x10^{1}+0x10^{0}  20  2X5^{1}+0x5^{0} 
…  …  …  … 
25  2x10^{1}+5x10^{0}  100  1x5^{2}+0x5^{1}+0x5^{0} 
26  2x10^{1}+6x10^{0}  101  1x5^{2}+0x5^{1}+1x5^{0} 
…  …  …  … 
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 twodimensional magic square your tuples will contain 2 digits. (A threedimensional magic cube would have tuples with 3 digits, a fourdimensional magic hypercube 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 4x5^{1}+4x5^{0}=24. So using twodigit base 5 numbers (written as tuples for the purpose of creating a magic square) gives us 25 different numbers (0 to 24=5^{2}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 4x5^{1}+4x5^{0}=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 leftmost 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 wraparound 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 n1. This is equal to n(n1)/2. Then since each tuple position really represents a digit in number written in base n, the magic number must be
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 wraparound the magic square. An alternative way to picture how a twodimensional square that wrapsaround 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 wraparound – 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 twodimensional Sudoku puzzle from a fourdimensional 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.
Comments
Magic Squares
Check out the Magic Square on the wall in the background of Albrecht Durer's famous print Melanchoia
http://en.m.wikipedia.org/wiki/Melencolia_I
Even Order Magic Squares
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?
Re: Even Order Magic Squares
I haven't yet found a method for working with even order magic squares.
Thanks for your comment.
Charlie
Maths of Squares
Very good article.
Re: Very Good Article
Thank you.
Charlie
The magic tori that display the magic squares
Thank you for this interesting article that mentions the torus construction. You can find more information about magic tori on the pages listed here:
https://carresmagiques.blogspot.fr/
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."
https://oeis.org/A270876
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."
https://oeis.org/A006052
2digits
Is there a formula for 3x3 squares that have 2digit numbers as well?
Who should I cite?
Did you create this method yourself, or is it a fairly common method?