Starting Value with the Longest Collatz Chain
Problem statement
For a positive value n, repeatedly replace an even value by n / 2 and an odd value by 3n + 1 until reaching 1.
Among starting values from 1 through limit, return the one with the longest chain, counting both the start and 1. Break equal lengths by the smaller starting value.
Function
longestCollatzStart(limit: int) → intExamples
Example 1
limit = 1return = 1Only starting value one is available.
Example 2
limit = 2return = 2Two has a longer chain than one.
Constraints
1 <= limit <= 1000000- Intermediate values fit in signed 64-bit integers.