-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem018.py
45 lines (29 loc) · 945 Bytes
/
problem018.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
def collatzUtil(number,lookup):
if number in lookup:
return lookup[number]
if number == 1:
lookup[number] = 1
return lookup[number]
elif number%2==0:
_number = number // 2
lookup[number] = 1 + collatzUtil(_number,lookup)
return lookup[number]
else:
_number = 3*number + 1
lookup[number] = 1 + collatzUtil(_number,lookup)
return lookup[number]
def collatzLen(N):
lookup = {}
max_long_collatz = 1
k_number = 0
collatzUtil(N,lookup)
for number in range(1,N):
if number not in lookup:
collatzUtil(number,lookup)
long_collatz_number = lookup[number]
if long_collatz_number > max_long_collatz:
max_long_collatz = long_collatz_number
k_number = number
print(k_number,max_long_collatz)
return (k_number,max_long_collatz)
collatzLen(1000000)