# How to perform Magic Square Operation in a Matrix using Python3

In this Python tutorial, we are going to learn how to perform a magic square operation in a matrix in Python. Here we will show you an easy example so that you can understand this tutorial easily.

## MAGIC SQUARE OPERATION IN PYTHON

A Magic Square is:

• The square is itself having smaller squares (same as a matrix) each containing a number.
• The numbers in each vertical, horizontal, and diagonal row add up to the same value.
• The dimension of the square matrix is an (odd integer x odd integer) e.g., 3×3, 5×5, 7×7.

An example of this is given below in the image, where the sumĀ is 15 for every column or row.

magic square

To know about this interesting puzzle, Magic Square on Wikipedia

Now, let’s take a look at the code.

### PROGRAM: Python program for magic square operation

```#Function
def generateSquare(n):
# 2-D array with all
# slots set to 0
magicSquare = [[0 for x in range(n)]
for y in range(n)]
# initialize position of 1
i = n / 2
j = n - 1
# Fill the square by placing values
num = 1
while num <= (n * n):
if i == -1 and j == n: # 3rd condition
j = n - 2
i = 0
else:
# next number goes out of
# right side of square
if j == n:
j = 0
# next number goes
# out of upper side
if i < 0:
i = n - 1
if magicSquare[int(i)][int(j)]: # 2nd condition
j = j - 2
i = i + 1
continue
else:
magicSquare[int(i)][int(j)] = num
num = num + 1
j = j + 1
i = i - 1 # 1st condition
# Printing the square
print ("Magic Square for n =", n)
print ("Sum of each row or column",n * (n * n + 1) / 2, "\n")
for i in range(0, n):
for j in range(0, n):
print('%2d ' % (magicSquare[i][j]),end = '')
# To display output
# in matrix form
if j == n - 1:
print()
# Driver Code
# Works only when n is odd
n=int(input("Number of rows of the Magic Square:"))
generateSquare(n)```

OUTPUT 1:

```Number of rows of the Magic Square:7
Magic Square for n = 7
Sum of each row or column 175.0

20 12  4 45 37 29 28
11  3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17  9  1
40 32 24 16  8  7 48
31 23 15 14  6 47 39
22 21 13  5 46 38 30```

OUTPUT 2:

```Number of rows of the Magic Square:5
Magic Square for n = 5
Sum of each row or column 65.0

9  3 22 16 15
2 21 20 14  8
25 19 13  7  1
18 12  6  5 24
11 10  4 23 17```

Also Read:

### 3 responses to “How to perform Magic Square Operation in a Matrix using Python3”

1. Steve Shambles says:

Hey, nice idea, I have shared on my Twitter.
It looks like the code will produce the same numbers
every time though, which makes it a bit limited,
I was thinking of doing a GUI on the code and
making a puzzle game from it, but I’m not
experienced enough yet to modify it to make
up different solutions each time.
Nice job though.

2. ar.drso says:

it just work for odd numbers.if you input an even number,You will encounter an error on the 25th line

3. Vivek Gupta says:

# Try this code…
A = [[0,0,0],[0,0,0],[0,0,0]]
count = 0
for c1 in range(1,10,1):
for c2 in range(1,10,1):
if c2==c1:
continue
for c3 in range(1,10,1):
if c3==c2 or c3==c1:
continue
for c4 in range(1,10,1):
if c4==c3 or c4==c2 or c4==c1:
continue
for c5 in range(1,10,1):
if c5==c4 or c5==c3 or c5==c2 or c5==c1:
continue
for c6 in range(1,10,1):
if c6==c5 or c6==c4 or c6==c3 or c6==c2 or c6==c1:
continue
for c7 in range(1,10,1):
if c7==c6 or c7==c5 or c7==c4 or c7==c3 or c7==c2 or c7==c1:
continue
for c8 in range(1,10,1):
if c8==c7 or c8==c6 or c8==c5 or c8==c4 or c8==c3 or c8==c2 or c8==c1:
continue
for c9 in range(1,10,1):
if c9==c8 or c9==c7 or c9==c6 or c9==c5 or c9==c4 or c9==c3 or c9==c2 or c9==c1:
continue
A[0][0]=c1
A[0][1]=c2
A[0][2]=c3
A[1][0]=c4
A[1][1]=c5
A[1][2]=c6
A[2][0]=c7
A[2][1]=c8
A[2][2]=c9
Check = True
if A[0][0]+A[0][1]+A[0][2]!=15:
Check=False #First Row
if A[1][0]+A[1][1]+A[1][2]!=15:
Check=False #Second Row
if A[2][0]+A[2][1]+A[2][2]!=15:
Check=False #Third Row
if A[0][0]+A[1][0]+A[2][0]!=15:
Check=False #First Column
if A[0][1]+A[1][1]+A[2][1]!=15:
Check=False #Second Column
if A[0][2]+A[1][2]+A[2][2]!=15:
Check=False #Third Column
if A[0][0]+A[1][1]+A[2][2]!=15:
Check=False #First Diagonal
if A[2][0]+A[1][1]+A[0][2]!=15:
Check=False #Second Diagonal
if Check:
count +=1
print(“Magic square number”, count)
for i in range(0,3,1):
for j in range(0,3,1):
print(A[i][j],end=” “)
print()
print()