A magic square is a square grid of numbers such that adding up the numbers along rows, columns, or diagonals yields the same sum. A 3x3 magic square, for instance, is comprised of nine cells and here is an example magic square where the magic sum is 15.

Where did magic squares come from?

Magic squares first appeared in Ancient China as early as 190 BC but only in the three by three variety. The concept then moved to India where four by four magic squares were constructed. Soon magic squares were a popular concept all around the world and so were the methods used to construct and solve them. Magic squares are still alive and popular today!

How do you make a magic square?

There are smart approaches that we can use to construct a magic square with consecutive numbers that we show later below in a Python program.

But a heuristic strategy is as follows. Simply pick a number (preferably a large one and let this be your magic sum. For our example below we will use a three by three magic square. Divide your number by three and place it in the middle cell. Next we will try out different numbers for the missing cells and keep switching them until we obtain a match. While this can be time consuming this is by far the most straightforward way.

How do you solve a magic square?

Solving a magic square means you are given some filled cells but other cells are unfilled, and your goal is to fill up all the given cells. You can use reasoning as well as trial and error to solve magic squares. We will walk you through how to solve the one below:

A good heuristic to solve magic square problems is to think of the middle cell which is part of many sums, along rows, columns, and diagonals. We can guess (and be right most of the time) that the middle cell will be a third of the magic sum. We can use this property to our advantage. We can call the middle cell “x”. From this we can construct the equation: x + 6 + 10 = 3x. From this we can deduce that x must be 8 and that the magic sum is 24.

Then we can continue with this magic sum in mind and fill in other cells to get the result as shown below:

Similar Concepts

There are a lot of concepts that are similar to the magic square such as the magic triangle. The magic triangle is composed of numbers along the edges all adding to the same number generally in the center of the triangle. There are also common math problems containing the idea of the magic square but not the shape.

Magic Square Generator in Python

To code a magic square generator we need to develop an algorithm first for making a magic square. We will show you one strategy, due to French diplomat de la Loubère. In honor of him, this method is called the de la Loubère method for constructing magic squares.

This method works only for magic squares of odd size, i.e., a 3-by-3, or 7-by-7. It will not work for magic squares of even size which need a different algorithm.

We begin the algorithm by placing the smallest number of the square in the first row, middle column. From that point onward we begin counting numbers incrementally and place them in successive cells.

How do you determine successive cells? The pattern is to look at the cell that is to the (right, above) with respect to the current cell. In other words, we move up one unit and to the right one unit.

In our current example, you will notice this takes us out of the square. In this case, we are still in a valid column but we have run out of rows. Because of this we will go down to the bottom row (keeping the same column) and put the next number in that spot.

Then we continue the process and place the next consecutive number in the cell that is to the (right, above) of the current cell. You will notice that we are again out of the square but this time we have run out of columns. So we will goto the first column (keeping the same row) and continue the process.

In case we end up with a cell that already has a number we instead place our next number in the cell immediately below the current cell. This rule also applies if you are in a corner and exit out of both columns and rows.

Now let's code our program in Python! The first part of our code asks the user for dimensions for the square as well as our starting number (remember that this will be placed in the first row, middle column).

We use numpy as the library of choice for storing our arrays in this program.

Next, in the below code we use numpy to create a two dimensional array (as to make a square) with the dimensions of the user's input and then make a second array for storing presence/absence values. We then check if our starting number is an integer and if it is, make the numpy array an integer array and to change our starting number which was a float to an integer.

Then we initialize the magic square by placing the starting number in the first row, middle column.

Next, we create a function that checks for various cases and places the next number in the appropriate cell.

The final step in our code uses our function we just created dimension ^2 times and then prints the numbers from the array one by one with dashes and other characters to represent the lines in an actual magic square. After this you are done!

Let us try to generate a 3-by-3 magic square:

Here is what the program generates when we attempt a 11-by-11 magic square:

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.