Data Processing and Visulisation with Python
Python Exercise 14
Check magic square
m = [[8, 1, 6], [3, 5, 7], [4, 9, 2]]
checkMagicSquare(m)
m = [[8, 1, 6], [4, 5, 7], [3, 9, 2]]
checkMagicSquare(m)
m = [[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]]
checkMagicSquare(m)
method 1
def checkMagicSquare(m):
n = len(m)
s = n*((1+n**2)/2)
## chech each row
for i in m:
c1 = 0
for j in range(n):
c1 += i[j]
if c1 != s:
return False
## chech each column
for i in range(n):
c2 = 0
for j in range(n):
c2 += m[j][i]
if c2 != s:
return False
c3 = 0
#diagonal
for i in range(n):
c3 += m[i][i]
if c3 != s:
return False
c4 = 0
#another diagonal
for i in range(n):
c4 += m[n-1-i][i]
if c4 != s:
return False
return True
Odd magic square
Background information
A magic square is an
N
×
N
N\times N
N×N square matrix whose numbers (usually integers) consist of consecutive numbers (usually from 1 to
N
2
N^2
N2) arranged so that the sums of each row and column, and both long (main) diagonals are equal to the same sum (which is called the magic number
or magic constant
).
When N is an odd number no less than 3, the magic square is called a magic square of odd order, or odd magic square.
The following is an order 5 magic square.
The magic number of an odd magic square can be easily calculated as:
(
1
+
N
2
)
×
N
2
\frac{(1+N^2) \times N}{2}
2(1+N2)×N
The most popular and most well-known method for creating odd magic squares was first published by Simon de la Loubère (1642-1729) and called De La Loubère Method:
- The number ‘1’ goes in the middle of the top row.
- All numbers are then placed one column to the right and one row up from the previous number.
- Whenever the next number placement is above the top row, stay in that column and place the number in the bottom row.
- Whenever the next number placement is outside of the rightmost column, stay in that row and place the number in the leftmost column.
- When encountering a filled-in square, place the next number directly below the previous number.
- When the next number position is outside both a row and a column, place the number directly beneath the previous number.
If you are more comfortable with Chinese, maybe you will like this:
一居上行正中央,
依次斜填切莫忘;
上出框时向下放,
右出框时向左放;
排重便在下格填,
右上排重一个样
For more information on odd magic squares and how to compose them, you can visit: http://www.1728.org/magicsq1.htm
Like more information in Chinese? you can visit: https://www.zhihu.com/question/30498489
Task
Write a Python function to take an odd integer N N N no less than 3, and return the N × N N \times N N×N matrix of magic square (in list of lists).
method 1
def oddMagicSquare(N):
l = []
for i in range(N):
l.append([])
for i in range(N):
for j in range(N):
l[i].append(0)
a = 0
b = int((N+1)/2-1)
for i in range(1,N**2+1):
l[a][b] = i
a -= 1
b += 1
if a == -1 and b == N:
a += 2
b -= 1
elif a == -1:
a = N-1
elif b == N:
b = 0
if l[a][b] != 0:
a += 2
b -= 1
return l
def printMagicSquare(m):
n = len(m)
s = len(str(n*n))
for r in m:
for c in r:
print(f'{c:{s}d}', end=' ')
print()
N = 3
m = oddMagicSquare(N)
print('Generated an odd magic square: ', checkMagicSquare(m))
printMagicSquare(m)
N = 5
m = oddMagicSquare(N)
print('Generated an odd magic square: ', checkMagicSquare(m))
printMagicSquare(m)
N = 11
m = oddMagicSquare(N)
print('Generated an odd magic square: ', checkMagicSquare(m))
printMagicSquare(m)
printMagicSquare(oddMagicSquare(23))