We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Lesson learned:
Caching is important for speed but don't cache everything. A naive approach is to use a built-in cache like "lru_cache" or set up your own cache using a dictionary to store everything. But there are too many numbers to cache; I kept track of the highest number occuring in a Collatz sequence after running N up to 5x10^6: 1318802294932. That number and other high numbers will likely never be called once they are cached. Set the cache to only store lengths for numbers less than 5 million or so. If a number is less than your chosen cache size then don't store it. Then, because your cache size is set, use a list instead of a dictionary to save space. I used Python 3.
Project Euler #14: Longest Collatz sequence
You are viewing a single comment's thread. Return to all comments →
Lesson learned: Caching is important for speed but don't cache everything. A naive approach is to use a built-in cache like "lru_cache" or set up your own cache using a dictionary to store everything. But there are too many numbers to cache; I kept track of the highest number occuring in a Collatz sequence after running N up to 5x10^6: 1318802294932. That number and other high numbers will likely never be called once they are cached. Set the cache to only store lengths for numbers less than 5 million or so. If a number is less than your chosen cache size then don't store it. Then, because your cache size is set, use a list instead of a dictionary to save space. I used Python 3.