-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_030_00.py
More file actions
136 lines (113 loc) · 3.31 KB
/
problem_030_00.py
File metadata and controls
136 lines (113 loc) · 3.31 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
##Starting with the number 1 and moving to the right
##in a clockwise direction a 5 by 5 spiral is formed
##as follows:
##
##21 22 23 24 25
##20 7 8 9 10
##19 6 1 2 11
##18 5 4 3 12
##17 16 15 14 13
##
##It can be verified that the sum of the numbers on the
##diagonals is 101.
##
##What is the sum of the numbers on the diagonals
##in a 1001 by 1001 spiral formed in the same way?
import numpy as np
import time
import functools
import math
import itertools
def euler28(size):
"""Generates a spiral within a square of -number- side"""
spire = np.zeros((size, size))
## print(spire)
n = 0
l = []
for i in spire:
for j in i:
n += 1
l.append(n)
direction = ""
last_position = ""
for element in l:
## print("here", size,element,spire, direction)
spire, direction, new_position = move(size,element,last_position,spire, direction)
## print(direction, new_position)
## print("new", spire)
last_position = new_position
print("END SPIRE:\n\n\n", spire)
a = np.trace(spire) + np.trace(spire[::-1]) - 1
print("Sum of diagonals = ", a)
def move(size,number,last_position, spire, direction):
if number == 1:
## print("inside move, number == 1")
i = size//2
j = size//2
spire[i][j] = number
direction = "E"
new_position = (i, j)
return spire, direction,new_position
## return spire, new_position
else:
## print("inside else, ", number, direction, last_position)
#move following direction, then check direction.
if direction == "N":
i = last_position[0] - 1
j = last_position[1]
elif direction == "E":
i = last_position[0]
j = last_position[1] + 1
elif direction == "S":
i = last_position[0] + 1
j = last_position[1]
elif direction == "W":
## print("inside W")
i = last_position[0]
j = last_position[1] - 1
else:
print("Else, ", direction, last_position)
print("Something went very wrong")
spire[i][j] = number
new_position = (i, j)
direction = checkdirection(new_position, spire, direction)
return spire, direction, new_position
def checkdirection(curr_pos, spire, curr_direction):
directions = ("N", "E", "S", "W")
i = curr_pos[0]
j = curr_pos[1]
new_dir = curr_direction
if curr_direction == "N" and spire[i][j+1] == 0:
new_dir = "E"
return new_dir
elif curr_direction == "E" and spire[i+1][j] == 0:
new_dir = "S"
return new_dir
elif curr_direction == "S" and spire[i][j-1] == 0:
return "W"
elif curr_direction == "W" and spire[i-1][j] == 0:
return "N"
else:
return new_dir
def euler30(n):
numbers = []
## itertools.count
for i in range(2,354294):
c = expon(i,n)
## print(i,c)
if c == i:
numbers.append(i)
print(sum(numbers))
def expon(N, n):
a = str(N)
b = 0
for i in a:
b += int(i)**n
## print(i, a, b)
return b
def main():
start = time.time()
euler30(5)
end = time.time()
print(end -start)
main()