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

Magic Square Calculator (1)

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

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

For n=3n=3n=3 we have M=15M=15M=15, for n=4n=4n=4 we have M=34M=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^21,2,...,n21,n2.

The magic square of order 111 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=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!

Magic Square Calculator (2)

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}a+b=Mc+d=Ma+c=Mb+d=M

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

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

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

Magic Square Calculator (3)

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 nmod4=2n\ \text{mod}\ 4=2nmod4=2; and
  • Magic squares with doubly even order, that is, multiples of 444, or nmod4=0n\ \text{mod}\ 4=0nmod4=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.

Magic Square Calculator (4)

Doubly even magic squares

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

  1. Start by placing the number 111 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(i1)n+j where nnn is the order of the square, iii the index of the row, and jjj the index of the column of the cell (in this case, (11)(4)+4=4(1-1)\cdot(4)+4=4(11)(4)+4=4.
  4. Proceed as if you were "reading" the square, filling the diagonals. The last number will be 161616 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=4n=4, 151515, 141414, 121212,...).

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.

Magic Square Calculator (5)

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 NNN into four odd order magic squares with order N2\tfrac{N}{2}2N, name them (in reading order) S1S_1S1, S3S_3S3, S4S_4S4, and S2S_2S2.
  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(i1)n2, where iii is the index of the square. For a square with N=6N=6N=6:
    • S1S_1S1 remains unchanged;
    • S2=S1+9S_2=S_1 + 9S2=S1+9;
    • S3=S1+18S_3=S_1 + 18S3=S1+18; and
    • S4=S1+27S_4=S_1 + 27S4=S1+27.

Let's take a look at the square now!

Magic Square Calculator (6)

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=1118+3+4+35+30+31=111;
  • 1+5+9+28+32+36=1111+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=848+1+6+26+19+24=84;

What?

  • 35+28+33+17+10+15=13835+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 kkk, a number that "generates" singly even magic squares of order NNN:

N=4k+2N=4\cdot k + 2N=4k+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-1k1 columns of the square.
  2. Swap all but the central element of the column kkk of the squares S1S_1S1 and S4S_4S4.
  3. Swap only the central element of the (k+1)th(k+1)^{\text{th}}(k+1)th column between the upper and lower square.

Take a look at the rearranged square:

Magic Square Calculator (7)

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

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

Magic Square Calculator (2024)

FAQs

How do you calculate the magic square? ›

How to Solve Magic square. As mentioned above, the formula of the magic square sum is n(n2 + 1)/2. For a magic square of order 3, we need to substitute n = 3 to know the magic sum so that we can easily form the magic square 3×3.

Is there a formula for magic squares? ›

To calculate the magic constant, add all nine numbers used in the magic square and divide by the number of rows. In our example, add 1+2+3+4+5+6+7+8+9 = 45, then divide by 3. The magic constant for this example is 15, as 45 / 3 = 15.

Is a 3x3 magic square always 15? ›

The pattern was a 3x3 grid of nine squares, each containing one of the numbers between 1 and 9. No matter which way the 3 numbers in each row, each column and both diagonals of the square were added, the sum was always 15. This arrangement is what we now know as the 3x3 magic square.

What is the rule for the 5x5 magic square? ›

Magic Square 5×5

In order to make a 5×5 magic square, the first step is to find the magic constant using the same formula: M=n(n2+1)2\(. So, the sum of all the numbers in each row, column, and main diagonal of a 5×5 magic square is 65.

What is the formula for the birthday magic square? ›

Ramanujan created the following birthday magic square from his date of birth (in DD MM YYYY format) where all four rows, four columns and two diagonals sum up to 139. e.g. sum of all rows equals to 139. 22 + 87 + 19 + 11 =139; 17 + 09 + 24 + 89 =139; That seems like magic and hence the name — magic square.

What is the algorithm for magic square? ›

A magic square A = [ a i j ] of degree is called symmetrical if the sum of each pair of two opposite entries a i j , a i j ∗ = a n + 1 − i , n + 1 − j with respect to the center is 2 ‖ A ‖ n , that is, a i j + a n + 1 − i , n + 1 − j = 2 ‖ A ‖ n .

How do you make a 10x10 magic square? ›

The Magic Grid for this 10x10 Square.

A single zero is followed by N/2 ones with the remainder of the row filled by zeros. The next line is the inverse. These two rows are repeated down the square. The final pair of rows, otherwise still the same, is reversed left for right.

Does 5 have to be in the middle of a magic square? ›

We now know that 5 must be in the center cell, so the number in the diagonally opposite cell from 1 must be 9. Now, in order to sum to 15 the top row must contain the numbers 1, 6, and 8. If either the 6 or 8 is in the corner cell then, since that column has 9 in its bottom cell, the sum of that column is more than 15.

What is the smallest 3x3 magic square? ›

441 is the smallest square that is the sum of three distinct squares in two non-intersecting ways. This solution also has the lowest possible maximum value of 157.

How to make 15 from 1 to 9? ›

Another approach is to consider the ways to obtain 15 as a sum of 3 different numbers in the range 1.. 9. These are: 1+5+9, 1+6+8, 2+4+9, 2+5+8, 2+6+7, 3+4+8, 3+5+7, 4+5+6.

What is the formula for the magic constant of a magic square? ›

The magic constant of a magic square of nth order can be found by dividing the sum of 1 to n2 by n; namely, the magic constant = n (n2+1)/2.

How is magic number calculated? ›

To find the magic number in baseball:
  • Take the total number of games, e.g., 162.
  • Subtract the total wins of the first team, e.g., 55.
  • Subtract the losses of the second team, e.g., 57.
  • Add 1 to the result and you have your first team's magic number, i.e., 51.
6 days ago

References

Top Articles
Latest Posts
Article information

Author: Nicola Considine CPA

Last Updated:

Views: 6302

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Nicola Considine CPA

Birthday: 1993-02-26

Address: 3809 Clinton Inlet, East Aleisha, UT 46318-2392

Phone: +2681424145499

Job: Government Technician

Hobby: Calligraphy, Lego building, Worldbuilding, Shooting, Bird watching, Shopping, Cooking

Introduction: My name is Nicola Considine CPA, I am a determined, witty, powerful, brainy, open, smiling, proud person who loves writing and wants to share my knowledge and understanding with you.