-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp028.py
More file actions
29 lines (27 loc) · 783 Bytes
/
Copy pathp028.py
File metadata and controls
29 lines (27 loc) · 783 Bytes
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
def spiral_cw(size):
res = 1
num = 1
# step size = grid_size - 1
# grid_size is odd - 3x3, 5x5, 7x7
# 2 4 6
for n in range(2, size, 2):
for i in range(4):
num += n
res += num
return res
def spiral_cw_formula(size):
res = 1
for t in range(3, size+1, 2):
"""
top right: t**2
top left: t**2 -(t-1)
bottom left: t**2 - 2*(t-1)
bottom right: t**2 - 3*(t-1)
"""
res += 4*(t**2) - 6*t + 6
return res
if __name__ == '__main__':
print('5x5 spiral: %d' % spiral_cw(5))
print('5x5 spiral: %d' % spiral_cw_formula(5))
print('1001x1001 spiral: %d' % spiral_cw(1001))
print('1001x1001 spiral: %d' % spiral_cw_formula(1001))