-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagic_square.py
37 lines (25 loc) · 1.08 KB
/
magic_square.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import numpy as np
print("Hi! Welcome to the magic square algorithm")
print("Please enter the number for which you would want a magic sqaure to be printed. Please enter an odd number")
# The odd number for which the magic square is created is stored in the variable N
N = int(input())
# create a matrix with values 0 using numpy. The datatype is int for the elements in the matrix
magic_square = np.zeros((N,N), dtype=int)
n = 1
i, j = 0, N//2
# n iterates from 1 to N**2. The loop exits when n is equal to N**2
while n <= N**2:
# Start in the middle of the first row.
# (i = 0 and j = N//2 ) and the element at magic_square[i,j] is the middle in the first row.
# insert n = 1 to begin with at magic_square[i,j]
magic_square[i, j] = n
# increment n by 1
n += 1
# Move diagonally up and right, wrapping to the first column or last row if the move leads outside the grid
new_i, new_j = (i-1) % N, (j+1)% N
# if the cell is already filled with a number, move vertically down one space.
if magic_square[new_i, new_j]:
i += 1
else:
i, j = new_i, new_j
print(magic_square)