99 lines
3.3 KiB
Plaintext
99 lines
3.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "43f43a42",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Longest Collatz Sequence\n",
|
|
"> The following iterative sequence is defined for the set of positive integers:\n",
|
|
"> * $n \\to n/2$ ($n$ is even)\n",
|
|
"> * $n \\to 3n + 1$ ($n$ is odd)\n",
|
|
"> \n",
|
|
"> Using the rule above and starting with 13, we generate the following sequence: \n",
|
|
"> $$13 \\to 40 \\to 20 \\to 10 \\to 5 \\to 16 \\to 8 \\to 4 \\to 2 \\to 1$$\n",
|
|
"> It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.\n",
|
|
"> \n",
|
|
"> Which starting number, under one million, produces the longest chain?\n",
|
|
"> \n",
|
|
"> **NOTE:** Once the chain starts the terms are allowed to go above one million.\n",
|
|
"\n",
|
|
"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."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "d1017296",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from functools import cache\n",
|
|
"\n",
|
|
"@cache\n",
|
|
"def collatz_length(n):\n",
|
|
" if n == 1:\n",
|
|
" return 1\n",
|
|
" elif n % 2 == 0:\n",
|
|
" return 1 + collatz_length(n // 2)\n",
|
|
" else:\n",
|
|
" return 1 + collatz_length(3 * n + 1)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "fd145a5b",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"837799"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"max(range(1, 1000000), key=collatz_length)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "SageMath 9.5",
|
|
"language": "sage",
|
|
"name": "sagemath"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|