This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathmagic_square.py
71 lines (54 loc) · 1.61 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 11:24:15 2020
@author: Gulshan
"""
'''
What is Magic Square?
=> a square divided into smaller squares each containing a number,
such that the figures in each vertical, horizontal, and diagonal row add up to the same value.
Example:
2 7 6
9 5 1
4 3 8
The sum of each row/column/diagonal is: 15.0
'''
# Function to create Magic Square
def magic_square(n):
magicSquare = []
for i in range(n):
listt = []
for j in range(n):
listt.append(0)
magicSquare.append(listt)
i = n//2
j = n-1
num = n*n
count = 1
while(count<=num):
if(i==-1 and j==n): #condition 4
j = n-2
i = 0
else:
if(j==n): # column value is exceeding
j = 0
if(i<0): # row is becoming -1
i=n-1
if(magicSquare[i][j]!=0):
j = j-2
i = i+1
continue
else:
magicSquare[i][j] = count
count+=1
i = i-1
j = j+1 #condition 1
for i in range(n):
for j in range(n):
print(magicSquare[i][j], end=" ")
print()
print("The sum of each row/column/diagonal is: "+str(n*(n**2+1)/2))
# Main Function for Magic Square
if __name__ == '__main__':
n = int(input("Enter Number To Generate Magic Square"))
magic_square(n)