I have been working through the euler project and I have created the following code for Problem 14, which asks which starting number, under 1 million, produces the longest Collatz Sequence chain.
It takes a good minute to process. Can I make it faster?
class Collatz:
def __init__(self):
self.c = 1
def if_even(self, n):
return int(n / 2)
def if_odd(self, n):
return int(n * 3 + 1)
def decide(self, n):
if (n & 1) == 0:
return self.if_even(n)
elif (n & 1) == 1:
return self.if_odd(n)
def iter_nums(self, n):
if n == 1:
return n
self.c += 1
n = self.decide(n)
self.iter_nums(n)
largest = 0
# col = Collatz()
# col.iter_nums(13)
# print(col.c)
for i in range(1000000, -1, -1):
col = Collatz()
col.iter_nums(i)
if col.c > largest:
largest = i
print(largest)
largest
, but storing the index, not the sequence length, inlargest
. Apples and oranges. \$\endgroup\$