-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_euler14.py
More file actions
executable file
·42 lines (34 loc) · 958 Bytes
/
project_euler14.py
File metadata and controls
executable file
·42 lines (34 loc) · 958 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
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/python3
def main():
prev_seq = {}
longest_seq = 0
starting_num = 0
for n in range(1, 1000000):
curr_seq = []
curr_n = n
curr_seq.append(curr_n)
while curr_n != 1:
try:
curr_seq += prev_seq[curr_n][1:]
break
except KeyError:
pass
if curr_n % 2 == 0:
curr_n = int(curr_n / 2)
else:
curr_n *= 3
curr_n += 1
curr_seq.append(curr_n)
for idx in range(0, len(curr_seq)):
try:
prev_seq[curr_seq[idx]]
break
except:
prev_seq[curr_seq[idx]] = curr_seq[idx:]
print(n, curr_seq)
if len(curr_seq) > longest_seq:
longest_seq = len(curr_seq)
starting_num = n
print(longest_seq, starting_num)
if __name__ == '__main__':
main()