Project Euler 14 - Longest Collatz sequence
Project Euler 14 - Longest Collatz sequence
Official link: https://projecteuler.net/problem=14
Thought Process
Thought Process
Create a function which quickly calculates the collatz sequence given a starting number, then I created an array where array[x] = len of collatz chain starting at x, I return the array.index(max(array))
Another way is to use the key=func key feature of max, so your code can simply be:
max(range(1,10^6), key=CollatzFunc)
This all happens in ~25 seconds, one nice trick is to notice that 3n+1 is even if n is odd, so can reduce 1 step, takes about ~5 seconds off
You can read more about it here: https://en.wikipedia.org/wiki/Collatz_conjecture
Interactive Code
Interactive Code
Input an integer (yourinput)
Code will output the starting number < yourinput, producing the longest chain