"The [Collatz conjecture](https://en.wikipedia.org/wiki/Collatz_conjecture) is a famous unsolved problem, and a classic example of a seemingly simple question that has proven very difficult, if not impossible, to answer.\n",
"\n",
"It's easy enough to *define* a [recursive](https://en.wikipedia.org/wiki/Recursion_(computer_science%29) function to calculate the chain length for a starting number $n$.\n",
"$$\n",
"f(n) = \\begin{cases}\n",
"1 & n = 1 \\\\\n",
"1+f(n/2) & n \\equiv 0 \\pmod{2} \\\\\n",
"1+f(3n+1) & n \\neq 1\\ \\text{and}\\ n \\equiv 1 \\pmod{2}\n",
"\\end{cases}\n",
"$$\n",
"However, we want its *implementation* to be efficient. We can optimize greatly if we cache the outputs we compute (the computer science term for this is [memoization](https://en.wikipedia.org/wiki/Memoization)). For instance, if we store the fact that $f(4) = 3$ after we've computed it, when we later compute $f(8) = 1 + f(4)$, the program can use the stored value of 3 rather than recomputing $f(4)$. For large inputs, this will save us (or really the computer, I guess) from redoing work.\n",
"\n",
"Python has a nice decorator called [cache](https://docs.python.org/3/library/functools.html) that will automagically memoize our function."
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."