-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagic_Square.py
57 lines (55 loc) · 1.7 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import numpy as np
def magic_squre(siz,array,low_b,up_b,lim,low):
num=low
array[int(1),int(siz//2)]=num
i=1
j=(siz//2)
num+=1
while num <=(lim-1):
if array[int(i-1),int(j+1)] != -1:
if array[int(i-1),int(j+1)]==0:
array[int(i-1),int(j+1)]=num
i=(i-1)
j=(j+1)
num+=1
elif array[int(i+1),int(j)]==0:
array[int(i+1),int(j)]=num
i=(i+1)
num+=1
else:
if (((i-1)<low_b) and ((j+1)>up_b-1)):
if array[int(up_b-1),int(1)]!=0:
array[int(i+1),j]=num
num+=1
i=i+1
else:
array[int(up_b-1),int(1)]=num
num+=1
j=1
i=up_b-1
elif ((((i-1)<=up_b-1) and ((i-1)>=low_b)) and (j+1>up_b-1)):
array[int(i-1),int(low_b)]=num
num+=1
i=i-1
j=low_b
elif (((i-1)<low_b) and (((j+1)>=low_b) and ((j+1)<=(up_b-1)))):
array[int(up_b-1),int(j+1)]=num
j=j+1
i=up_b-1
num+=1
print(array[low_b:up_b,low_b:up_b])
n = int(input("Enter the dimension:"))
if n%2 != 0:
array=np.zeros([n+2,n+2])
sizee=n+2
low=int(input("Enter lower end number:"))
array[0,:]=-1
array[sizee-1,:]=-1
array[:,0]=-1
array[:,sizee-1]=-1
low_bound=1
up_bound=(sizee-1)
lim=(n**2)+low
magic_squre(n+2,array,low_bound,up_bound,lim,low)
else:
print("Enter an odd dimension, please!")