Ask me anything about your lessons or your code.
“What is a port?” · “Why is my code erroring?”
Monday, November 30
Find the starting number below one million with the longest Collatz chain.
The Collatz sequence starts from any positive whole number and repeatedly applies one rule until it reaches 1:
For example, starting at 13 the chain runs:
13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1That chain has 10 terms. It is believed (though not proven) that every starting number eventually reaches 1.
Your job: among all starting numbers below 1,000,000, find the one that produces the longest chain. Submit that starting number, not the length of its chain.
A naive approach recomputes the same tails millions of times and is far too slow. The trick is memoization: keep a dictionary mapping a number to its chain length. When you compute a new starting number, walk it forward until you hit a value already in the dictionary, then fill in the length for every value you passed through on the way. Each number gets its length computed exactly once, which makes the full scan fast.
Run your code in the editor, read the starting number it prints, and submit it.