Cody Bable

Generation

generate functionWed, 12 Apr 2023

create a main driver than repeatedly calls collatz() and determines the length of the returned list.

def collatz_length(num): length = 1 while num != 1: if num % 2 == 0: num = num // 2 length += 1 else: num = 3 * num + 1 length += 1 return length def main(): max_length = 1 max_length_num = 1 for i in range(1, 1000000): if collatz_length(i) > max_length: max_length = collatz_length(i) max_length_num = i return max_length_num if __name__ == "__main__": print(main())

Questions about programming?Chat with your personal AI assistant