forked from rahulinux/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollatz_problem.py
More file actions
31 lines (24 loc) · 855 Bytes
/
collatz_problem.py
File metadata and controls
31 lines (24 loc) · 855 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
#!/usr/bin/env python
def apply_rule(n):
if n%2 == 0: # if n is even
return n/2
elif n%2 == 1: # if n is odd
return ( n*3 ) + 1
return "No rule for this number"
def generate_chain(n):
my_list = []
while True:
n = apply_rule(int(n))
my_list.append(n)
if my_list[-1] == 1:
return my_list
my_list = []
break
# find logest chain
# Since 1 million calculation hang your lappy let's break into 3 sets
set_1 = max([ generate_chain(l) for l in range(1,333333)],key=len)
set_2 = max([ generate_chain(l) for l in range(333333,666666)],key=len)
set_3 = max([ generate_chain(l) for l in range(666666,1000000)],key=len)
logest_chain = max( [ set_1,set_2,set_3],key=len)
print logest_chain
print "Logest list is of {} integer and length is {}".format(logest_chain[0]/3,len(logest_chain))