-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicsquare.py
44 lines (38 loc) · 951 Bytes
/
magicsquare.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
def magic(n):
print("Study The Rule First Then start Program")
magic=[]
for i in range(n):
l=[]
for j in range(n):
l.append(0)
'''l.append(j) is one of mistake'''
magic.append(l)
i=n//2
j=n-1
count=n*n
sum=int((n*(n**2 +1))/2)
print("Sum Of The Matrix:",sum)
ini=1
while(count>=ini):
if(i==-1 and j==n):
i=0
j=n-2
if(i==-1):
i=n-1
if(j==n):
j=n-n
if(magic[i][j]!=0):
i=i+1
j=j-2
else:
magic[i][j]=ini
ini=ini+1
i=i-1
j=j+1
for i in range(n):
for j in range(n):
print(magic[i][j],end=" ")
print()
print("Magic Square Is only For Odd Numbers")
n=int(input("Enter Square Matrix Size:"))
magic(n)