python-euler/solutions/014/longestcollatzsequence.py

19 lines
448 B
Python
Raw Normal View History

2023-07-31 16:03:10 -04:00
def collatz_to_one(n):
chainLength = 1
while (n > 1):
if n % 2 == 0:
n = n/2
else:
n = 3 * n + 1
chainLength += 1
return chainLength
longestChain = 0
for i in range(1000000):
currentChain = collatz_to_one(i)
if currentChain > longestChain:
longestChain = currentChain
print("current longest chain: " + str(longestChain) + " (from " + str(i) + ")")
print("done")