-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler14b.py
57 lines (38 loc) · 1.24 KB
/
euler14b.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
"""
Euler 14
n > n/2 (n is even)
n > 3n + 1 (n is odd)
for which number, n, can we produce the longest chain?
in this I have attempted to use a more complete cache, involving all the numbers that are generated by the generate_list
function but the process of creating the list open ended list is slowing everything down...
stupid append....
"""
import itertools
import time
start = time.time()
cache = [0] * 1000001
cache[1] = 1
def generate_list(num, cache):
plus = 0
_list = [num]
while True:
if num <= 1:
break
if num < 1000000 and cache[num] != 0:
plus = cache[num]
break
if num % 2 == 0:
num = num / 2
else:
num = (3 * num) + 1
_list.append(num)
numbered_list = [(x, y) for (x, y) in itertools.izip(_list, (i + plus for i in reversed(xrange(len(_list))))) if x < 1000000]
return numbered_list
for num in xrange(1000000):
if cache[num] != 0:
continue
for (x, y) in generate_list(num, cache):
cache[x] = y
elapsed = (time.time() - start)
print "Found %d at %d in %s seconds" % (max(cache), cache.index(max(cache)), elapsed)
print cache[837799]