problem014.py (798B)
1 # problemName = "Longest Collatz sequence" 2 # problemNum = 14 3 # solutionBy = "FIGBERT" 4 # language = "Python" 5 # dateCompleted = "06/02/2020" 6 7 def collatz(num): 8 sequence = 1 9 while num > 1: 10 if num % 2 == 0: 11 num = num//2 12 else: 13 num = (3*num) + 1 14 sequence += 1 15 return sequence 16 17 if __name__ == "__main__": 18 answer = [999999, 999999, collatz(999999)] 19 while answer[0] >= 1: 20 potential = (answer[0] - 1, collatz(answer[0] - 1)) 21 if potential[1] > answer[2]: 22 answer[1] = potential[0] 23 answer[2] = potential[1] 24 answer[0] = answer[0] - 1 25 print(( 26 "The starting number, under one million, that produces the " 27 f"longest chain from the given iterative sequence is {answer[1]}" 28 ))