"""Step 1: reproduce the known answer to starter problem X1. Collatz map: n -> n/2 if n is even, 3n+1 if n is odd. Among the starting numbers below 1,000,000, find the one that takes the most steps to reach 1. Run: python3 verify_1_starter.py Uses only the Python standard library. Exits with status 1 if the result is not the expected one. """ import sys LIMIT = 1_000_000 EXPECTED_START = 837799 EXPECTED_STEPS = 524 def main(): # steps[n] = number of steps from n down to 1. Filled in increasing order, so the moment a trajectory # drops below its starting number the rest of the count is already known. steps = [0] * LIMIT best_start, best_steps = 1, 0 for n in range(2, LIMIT): x, count = n, 0 while x >= n: x = x // 2 if x % 2 == 0 else 3 * x + 1 count += 1 steps[n] = count + steps[x] if steps[n] > best_steps: best_start, best_steps = n, steps[n] print(f"longest Collatz chain below {LIMIT}: start {best_start}, {best_steps} steps") if (best_start, best_steps) != (EXPECTED_START, EXPECTED_STEPS): print(f"FAIL: expected start {EXPECTED_START} with {EXPECTED_STEPS} steps") return 1 print("PASS") return 0 if __name__ == "__main__": sys.exit(main())