List :Data Processing and Visulisation with Python (Python Exercise 14)

Data Processing and Visulisation with Python

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:

  1. The number ‘1’ goes in the middle of the top row.
  2. All numbers are then placed one column to the right and one row up from the previous number.
  3. Whenever the next number placement is above the top row, stay in that column and place the number in the bottom row.
  4. Whenever the next number placement is outside of the rightmost column, stay in that row and place the number in the leftmost column.
  5. When encountering a filled-in square, place the next number directly below the previous number.
  6. 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))

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值