101 lines
3.1 KiB
Plaintext
101 lines
3.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "43f43a42",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Longest Collatz Sequence](https://projecteuler.net/problem=14)\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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "bc0666ce",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Relevant sequences\n",
|
|
"* Collatz chain lengths: [A008908](https://oeis.org/A008908)\n",
|
|
"\n",
|
|
"#### Copyright (C) 2025 filifa\n",
|
|
"\n",
|
|
"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)."
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|