Exercise 2: The Collatz Sequence — Possible Solution ==================================================================== n = 6 steps = 0 sequence = [n] while n != 1: if n % 2 == 0: n //= 2 else: n = 3 * n + 1 sequence.append(n) steps += 1 print(" -> ".join(str(value) for value in sequence)) print(f"{steps} steps") Example run: 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 8 steps WHY THIS WORKS AS AN ANSWER ------------------------------ Each pass changes n, and the condition n != 1 is tested again with the new value. Use // (whole-number division) for the halving so n stays an integer. The sequence list collects every value including the starting one, and join() needs strings, which is why each value goes through str(). Starting from 6 the sequence takes 8 steps. Starting from 27 it takes 111 steps and climbs as high as 9232 before falling back to 1. Nobody has proved that every starting number reaches 1, so this is an unproven conjecture, but no counterexample has been found.