# Magic Square Calculator

*“Magic Square Construction Algorithms and Their Applications“*See 2 more sources

*“On the Construction of Doubly Even Order Magic Squares“*William H. Richardson

*“Magic Squares of Even Order (4n + 2)“*

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

The sum result is the **magic constant** $M$, and its value depends on the **order** $n$ of the magic square.

For $n=3$ we have $M=15$, for $n=4$ we have $M=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,...,n^2-1,n^2$.

The **magic square of order** $1$ 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=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!

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 $M$, so we can write:

Which implies $b=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 $1$ there. - Move
**one square up**, and**one square left**, and place the next number, $2$.- We are out of the square: imagine to "tile" around it with its copies, and place the number $2$ 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 $n\ \text{mod}\ 4=2$; and - Magic squares with
**doubly even order**, that is,**multiples**of $4$, or $n\ \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.

#### Doubly even magic squares

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

- Start by placing the
**number**$1$ 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 $(i-1) \cdot n + j$ where $n$ is the order of the square, $i$ the index of the row, and $j$ the index of the column of the cell (in this case, $(1-1)\cdot(4)+4=4$.
- Proceed as if you were "reading" the square, filling the diagonals. The last number will be $16$ 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 $n=4$, $15$, $14$, $12$,...).

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 $N$ into**four**odd order magic squares with order $\tfrac{N}{2}$, name them (in reading order) $S_1$, $S_3$, $S_4$, and $S_2$.- Use the algorithm for odd order magic squares to
**fill**them, then sum the number in each square to $(i-1)\cdot n^2$, where $i$ is the index of the square. For a square with $N=6$:- $S_1$ remains unchanged;
- $S_2=S_1 + 9$;
- $S_3=S_1 + 18$; and
- $S_4=S_1 + 27$.

Let's take a look at the square now!

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

- $8+3+4+35+30+31=111$;
- $1+5+9+28+32+36=111$;

And so on.

Now check the rows:

- $8+1+6+26+19+24=84$;

What?

- $35+28+33+17+10+15=138$.

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

Let's introduce $k$, a number that "generates" singly even magic squares of order $N$:

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**$k-1$ columns of the square.**Swap****all but the central element**of the column $k$ of the squares $S_1$ and $S_4$.**Swap****only the central element**of the $(k+1)^{\text{th}}$ 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.

- $8+28+33+17+10+15=111$;
- $35+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>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 $x$ and $n-x+1$ followed by the swap of the same index rows. In an even magic square there are $\tfrac{n}{2}$ such columns, in an odd one $\tfrac{n-1}{2}$: the resulting combinations are respectively $2^{\tfrac{n}{2}}$ and $2^{\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 $2^{\tfrac{n\cdot(n-2)}{8}}$ for even orders and $2^{\tfrac{(n-1)\cdot(n-3)}{8}}$ for odd orders. - A
**generalized exchange**, where we swap any pair of**non-central**rows (columns) $x$ and $y$, alongside a swap of the rows (columns) $n-x+1$ and $n-y+1$. If $x$ and $y$ are on the same side (and $x \neq y$). This is the second case we've seen. If $x=n-y+1$ we have a "diagonal" exchange.

- The exchange of a pair of
**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`

.

- The center is
`5`

(it appears in four sums). - Since the number in the center is odd, and
`15`

is 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`

.

8 | 3 | 4 |

1 | 5 | 9 |

6 | 7 | 2 |