Omni Calculator logo

Magic Square Calculator

Created by Davide Borchia
Reviewed by Anna Szczepanek, PhD and Jack Bowater
Based on research by
Krishnappa H K, N K Srinath and Ramakanth Kumar PMagic Square Construction Algorithms and Their Applications;See 2 more sources
Grasha Jacob, Dr. A. MuruganOn the Construction of Doubly Even Order Magic Squares;William H. RichardsonMagic Squares of Even Order (4n + 2);
Last updated: Jan 18, 2024


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.

The magic square is not you everyday square; for that one, visit the square calculator! And if you are looking for perfect but not magic squares, we've got you: try the perfect square calculator😉

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 n21n^2-1, where nn 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 nn; and
    • Cells separated by n2\tfrac{n}{2} on a diagonal sum to n2+1n^2+1.

🔎 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.

Why stop at squares, though? Mathematicians have sprinkled magic over circles, triangles, hexagons, and many other shapes. Triangular numbers are not magic, but still interesting: discover them at our triangular numbers calculator.

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 permutation (visit our permutation calculator for a quick reminder), 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.

Summing magic squares
The rows, columns, and diagonals of a "normal" magic square sum to the same value!

The sum result is the magic constant MM, and its value depends on the order nn of the magic square.

M=n(n2+1)2M=\frac{n\cdot (n^2+1)}{2}

For n=3n=3 we have M=15M=15, for n=4n=4 we have M=34M=34, and so on. However, a magic square remains magical when each of its numbers is summed to or multiplied by a constant (as in our matrix by scalar calculator). 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 1,2,...,n21,n21,2,...,n^2-1,n^2.

The magic square of order 11 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 n=2n=2 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!

2 by 2 magic square
The impossible 2 by 2 magic square.

Take a generic 2 by 2 magic square, and write the possible sums for the rows and columns:

{a+b=Mc+d=Ma+c=Mb+d=M\begin{cases} a+b=M\\ c+d=M\\ a+c=M\\ b+d=M \end{cases}

Now consider the first and the third equations. They both equal MM, so we can write:

a+b=a+ca+b=a+c

Which implies b=cb=c: 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 11 there.
  • Move one square up, and one square left, and place the next number, 22.
    • We are out of the square: imagine to "tile" around it with its copies, and place the number 22 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!

Building a 3x3 ms
How to build a 3 by 3 magic 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 n mod 4=2n\ \text{mod}\ 4=2; and
  • Magic squares with doubly even order, that is, multiples of 44, or n mod 4=0n\ \text{mod}\ 4=0.

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.

4by4 magic square
Building a 4 by 4 magic square.

Doubly even magic squares

To fill a doubly even magic square, you must follow the diagonals!

  1. Start by placing the number 11 in the upper left corner.
  2. Moving as if you were reading, identify the next cell lying on one of the diagonals.
  3. Fill the cell with the value (i1)n+j(i-1) \cdot n + j where nn is the order of the square, ii the index of the row, and jj the index of the column of the cell (in this case, (11)(4)+4=4(1-1)\cdot(4)+4=4.
  4. Proceed as if you were "reading" the square, filling the diagonals. The last number will be 1616 in the lower right corner.
  5. Return to the topmost row and fill the empty squares with the remaining numbers in descending order (in the case of n=4n=4, 1515, 1414, 1212,...).

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.

8 by 8 magic square
This is how an 8 by 8 magic square looks. We highlighted the 4 by 4 squares and their diagonals, respectively in red and blue.

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:

  1. Divide the magic square with order NN into four odd order magic squares with order N2\tfrac{N}{2}, name them (in reading order) S1S_1, S3S_3, S4S_4, and S2S_2.
  2. Use the algorithm for odd order magic squares to fill them, then sum the number in each square to (i1)n2(i-1)\cdot n^2, where ii is the index of the square. For a square with N=6N=6:
    • S1S_1 remains unchanged;
    • S2=S1+9S_2=S_1 + 9;
    • S3=S1+18S_3=S_1 + 18; and
    • S4=S1+27S_4=S_1 + 27.

Let's take a look at the square now!

6 by 6 magic square
The first step to fill a singly even magic square.

It looks neat, doesn't it. Well, let's check the sum of the columns:

  • 8+3+4+35+30+31=1118+3+4+35+30+31=111;
  • 1+5+9+28+32+36=1111+5+9+28+32+36=111;

And so on.

Now check the rows:

  • 8+1+6+26+19+24=848+1+6+26+19+24=84;

What?

  • 35+28+33+17+10+15=13835+28+33+17+10+15=138.

Oh no! We need to shuffle the numbers, but just a little.

Let's introduce kk, a number that "generates" singly even magic squares of order NN:

N=4k+2N=4\cdot k + 2

To correctly fill singly even magic square follow these three final rules.

  1. Swap the elements of the upper and lower squares in the first and last k1k-1 columns of the square.
  2. Swap all but the central element of the column kk of the squares S1S_1 and S4S_4.
  3. Swap only the central element of the (k+1)th(k+1)^{\text{th}} column between the upper and lower square.

Take a look at the rearranged square:

Rearranged 6 by 6 square
After a bit of reshuffling, our magic square is magic again!

Let's check the sums of the rows that were wrong before.

  • 8+28+33+17+10+15=1118+28+33+17+10+15=111;
  • 35+1+6+26+19+24=11135+1+6+26+19+24=111.

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 n>3n>3, 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 xx and nx+1n-x+1 followed by the swap of the same index rows. In an even magic square there are n2\tfrac{n}{2} such columns, in an odd one n12\tfrac{n-1}{2}: the resulting combinations are respectively 2n22^{\tfrac{n}{2}} and 2n122^{\tfrac{n-1}{2}}.
    • 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 2n(n2)82^{\tfrac{n\cdot(n-2)}{8}} for even orders and 2(n1)(n3)82^{\tfrac{(n-1)\cdot(n-3)}{8}} for odd orders.
    • A generalized exchange, where we swap any pair of non-central rows (columns) xx and yy, alongside a swap of the rows (columns) nx+1n-x+1 and ny+1n-y+1. If xx and yy are on the same side (and xyx \neq y). This is the second case we've seen. If x=ny+1x=n-y+1 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!

FAQ

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 15.

  1. The center is 5 (it appears in four sums).
  2. Since the number in the center is odd, and 15 is odd too, each pair of numbers on its side has same evenness.
  3. All the pairs of numbers encircling the center sum to 10. There are four combinations: 1 + 9, 2 + 8, 3 + 7, and 4 + 6.
  4. 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.

Davide Borchia
Size
You can download the magic square as a text file, CSV ready. Do you want to proceed?
Download
No
New square
Yes
618
753
294
Check out 75 similar arithmetic calculators ➗
Absolute changeAbsolute valueAdding and subtracting fractions… 72 more
People also viewed…

Books vs e-books

Discover the ultimate paper books vs. e-books calculator. Make an informed choice with our handy tool. Find out which is best for the environment!

Cos 2 theta

This cos 2 theta calculator will teach you all there is about the double angle formula for the cosine function.

Plastic footprint

Find out how much plastic you use throughout the year with this plastic footprint calculator. Rethink your habits, reduce your plastic waste, and make your life a little greener.

Pyramid volume

This pyramid volume calculator helps in finding the volume of a pyramid with a regular base or with a known base area.
Copyright by Omni Calculator sp. z o.o.
Privacy, Cookies & Terms of Service