-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler028.py
53 lines (47 loc) · 1 KB
/
euler028.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
def spiral(N):
M = [[0 for x in range(N)] for y in range(N)]
M[N // 2][N // 2] = 1
x = N // 2
y = N // 2
n = 1
M[x][y] = n
#print(M)
for c in range(1, (N // 2) + 1):
n += 1
y += 1
M[x][y] = n
# NW -> SW
for i in range(1, c + c):
n += 1
x += 1
M[x][y] = n
#print(M)
# SW -> SE
for i in range(1, 2 * c +1 ):
n += 1
y -= 1
M[x][y] = n
#print(M)
# SE -> NE
for i in range(1, 2 * c + 1):
n += 1
x -= 1
M[x][y] = n
#print(M)
# NE -> NW
for i in range(1, 2 * c + 1):
n += 1
y += 1
M[x][y] = n
#print(M)
return M
def sumDiagonal(M):
s = 0
l = len(M[0])
for i in range(l):
s += M[i][i] + M[i][l - i - 1]
if l % 2 != 0:
C = l // 2
s -= M[C][C]
return s
print(sumDiagonal(spiral(1001)))