Magic Square Calculator
Sprinkle some magic in your math with our magic square calculator! In this calculator, we will lead you in an exploration of this interesting mathematical puzzle.
Here you will learn:
- What is a magic square?
- The history of magic squares;
- The properties of magic squares;
- How to calculate a magic square (with a step by step guide on the calculation of some of them);
- Where is the 2 by 2 magic square; and
- How to shuffle a magic square.
And much more, from the weird types of magic squares to how many 3x3 magic squares are there. Abracadabra!
What is a magic square?
A magic square is a square grid of integer numbers arranged in such a way that their positions respect three rules:
- No numbers repeat;
- The summation the numbers in each row and column returns the same value; and
- The sum of the values in both diagonals is equal to the values of summed rows and columns.
However, the magic doesn't end here. It is possible to arrange numbers in such a way to satisfy other rules - this generates different types of magic squares. We then have:
- Pandiagonal magic squares - Where the sum of the numbers on a broken diagonal is the same as the sums of the rows and columns;
- Associative magic squares - Where cells symmetrical with respect to the center sum to the value of , where is the order of the square; and
- Most-perfect magic squares - This square is both pandiagonal and:
- Each 2 by 2 subsquare has a total sum equal to a quarter of the value of its rows divided by ; and
- Cells separated by on a diagonal sum to .
🔎 A broken diagonal is an offset diagonal that "wraps" around the corner of a square. To visualize it, imagine putting two identical magic squares side by side: a broken diagonal would start in one and end in the next.
Generalizing a magic square in higher dimensions gives us the magic hypercubes, where the sums are generalized over the various faces of the solid and the spatial diagonals. But this is definitely too complicated. Back to squares one! 😉
The history of magic squares
Where there's magic, there's the attention of humanity, and magic squares are no exception. The first traces of magic squares date back to 190 BC, where the first 3 by 3 magic square appears in the record. In the following centuries, many cultures started exploring these mathematical curiosities.
🙋 We don't think it was necessary, but here's a disclaimer anyway: magic squares are not magic! It's only a matter of numbers and math - close to magic, but still real!
Magic squares landed in Europe, where mathematicians quickly removed any esoterism (always the killjoys) and expanded the theoretical knowledge around the topic. Magic squares turned into a mathematical curiosity, with studies on the possible permutations, their properties, and variations.
Properties of magic squares
Magic squares are all about addition. We already saw where to look for sums in the introduction, but here's a diagram to make things clear.
The sum result is the magic constant , and its value depends on the order of the magic square.
For we have , for we have , and so on. However, a magic square remains magical when each of its numbers is summed to or multiplied by a constant. In that case, the magic constant can be bigger. This article and our magic square calculator will only consider magic squares populated by the numbers .
The magic square of order is trivial: this means that it theoretically satisfies the requirements to be a magic square, but... meh.
The elusive 2 by 2 magic square
If something goes wrong in physics or math, it's likely that a factor two went missing somewhere. That hideous number decided to ruin magic squares for everyone, too.
The value is, in fact, the only one for which we can't build a magic square. You can try to understand why by yourself (it's pretty easy), or you can read below!
Take a generic 2 by 2 magic square, and write the possible sums for the rows and columns:
Now consider the first and the third equations. They both equal , so we can write:
Which implies : since there can't be identical number in a magic square, we can't build a 2 by 2 specimen!
How to calculate a magic square: from 3x3 magic squares to infinity
Building a magic square is not difficult! There are different algorithms that you can use: here we will explain how to build every possible magic square!
Odd magic squares
This is probably the easiest magic square to build on a piece of paper. Take a pen and follow the steps! We will start calculating a 3 by 3 magic square:
- Chose the cell in the middle of a side (we chose the top one), and place the number there.
- Move one square up, and one square left, and place the next number, .
- We are out of the square: imagine to "tile" around it with its copies, and place the number in the corresponding place of the original square.
- Proceed in this fashion until the movement brings you to an occupied square. In that case, move down one square.
After nine iterations, you should have filled the square!
For magic squares with even order the process is slightly more complex. We need to identify two possible behaviors:
- Magic squares with singly even order, which means ; and
- Magic squares with doubly even order, that is, multiples of , or .
We will begin with doubly even magic squares.
🔎 We included an animation to show you the sequence of steps you have to take. The animation is for a 4×4 magic square, but the algorithm is valid for any order.
Doubly even magic squares
To fill a doubly even magic square, you must follow the diagonals!
- Start by placing the number in the upper left corner.
- Moving as if you were reading, identify the next cell lying on one of the diagonals.
- Fill the cell with the value where is the order of the square, the index of the row, and the index of the column of the cell (in this case, .
- Proceed as if you were "reading" the square, filling the diagonals. The last number will be in the lower right corner.
- Return to the topmost row and fill the empty squares with the remaining numbers in descending order (in the case of , , , ,...).
For any other doubly even magic square, you have to subdivide the square into the adequate number of 4×4 squares, fill all of their diagonals (thus creating a sort of tilted reticulate over the square), finally filling the empty squares, disregarding the subdivision.
As you can see in the 8×8 magic square, the filling process is not difficult at all.
Singly even magic squares
The last algorithm we use in our magic square calculator allows you to fill singly even magic squares.
We resort again to a subdivision in smaller magic squares, but this time of odd order. Follow our steps:
- Divide the magic square with order into four odd order magic squares with order , name them (in reading order) , , , and .
- Use the algorithm for odd order magic squares to fill them, then sum the number in each square to , where is the index of the square. For a square with :
- remains unchanged;
- ; and
Let's take a look at the square now!
It looks neat, doesn't it. Well, let's check the sum of the columns:
And so on.
Now check the rows:
Oh no! We need to shuffle the numbers, but just a little.
Let's introduce , a number that "generates" singly even magic squares of order :
To correctly fill singly even magic square follow these three final rules.
- Swap the elements of the upper and lower squares in the first and last columns of the square.
- Swap all but the central element of the column of the squares and .
- Swap only the central element of the column between the upper and lower square.
Take a look at the rearranged square:
Let's check the sums of the rows that were wrong before.
You can check the others, but trust us: the magic is back!
Pulling squares out of a hat
Depending on the order of the square, there can be many possible combinations that fulfill the magic requirements. Some of them are "trivial", others a bit more complex.
Let's take a closer look at some of them.
There is a single way to calculate a 3 by 3 magic square with the algorithm we've already seen. We can rotate and reflect it, but the relative positions of the number wouldn't change. We say that the 3x3 magic square is unique.
The story is different for higher orders. For , we can transform a magic square into another by:
- Rotation: four possible magic squares.
- Reflection: by mirroring, you can create three magic squares.
- Combination of rotations and reflections (you may find duplicates)!
- Exchange of columns and rows. We can identify:
- The exchange of a pair of rows and columns intersecting on a diagonal. We swap the columns and followed by the swap of the same index rows. In an even magic square there are such columns, in an odd one : the resulting combinations are respectively and .
- The exchange of a pair of rows and columns on the same side of the center with the corresponding pairs on the other side. The squares we can obtain are for even orders and for odd orders.
- A generalized exchange, where we swap any pair of non-central rows (columns) and , alongside a swap of the rows (columns) and . If and are on the same side (and ). This is the second case we've seen. If we have a "diagonal" exchange.
- Dividing the square into four sub-squares and swapping them with their opposite across the center.
How to use our magic square calculator
Our magic square calculator allows you to create a magic square of any (possible) size. You only have to choose the size of the square, say the magic word ("math!"). We will generate the square, and if the size is too big, we will ask you if you really want to visualize it.
In any case, we give you the possibility of downloading the magic square as a text file - if you know how to code and you want to play a little with magic, we can give you the raw materials!
What is a magic square?
A magic square is a grid of numbers where the sum of the values in every row, column, and diagonal equals a magic constant. Magic squares appeared early in history and then pervaded mathematics with their interesting properties.
How many 3x3 magic squares are there?
Accounting for rotations and reflections, there is a single way to fill a 3x3 magic square: it is unique. For larger magic squares, the number of possibilities grows: for a 4 by 4 magic square, we can find 880 distinct solutions.
How to fill a 3 by 3 magic square?
You can fill a 3×3 magic square using an algorithm or by intuition. The sum of rows, columns, and diagonals is
- The center is
5(it appears in four sums).
- Since the number in the center is odd, and
15is odd too, each pair of numbers on its side has same evenness.
- All the pairs of numbers encircling the center sum to
10. There are four combinations:
1 + 9,
2 + 8,
3 + 7, and
4 + 6.
- Fill the square by starting with a pair and checking the sums!
What is the magic constant of a 3 by 3 magic square?
The magic constant, that is the sum of the numbers in each row, column, and diagonal of a 3 by 3 magic square, is
15. To find it, sum all of the numbers in the square:
1 + 2 + 3 + ... + 9 = 45
And divide the result by the number of rows (or columns):
45/3 = 15.