eulerbooks/notebooks/problem0086.ipynb

129 lines
3.5 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "c102f0bb",
"metadata": {},
"source": [
"# [Cuboid Route](https://projecteuler.net/problem=86)\n",
"\n",
"Suppose you have a cuboid with side lengths $a \\leq b \\leq M$. Then the shortest route will be $\\sqrt{(a + b)^2 + M^2}$. We're interested in when this distance is an integer.\n",
"\n",
"However, rather than iterate through values of $a$, $b$, and $M$, we can be more efficient by iterating through values of $M$, then values of $s$, where $s \\leq 2M$. If $s^2 + M^2$ is a square number, then that means any $a,b$ such that $s = a+b$ and $a \\leq b \\leq M$ will correspond to an $a \\times b \\times M$ cuboid with integer shortest route.\n",
"\n",
"So, if $s = a + b$, naturally $b = s - a$, and we want to know how many values of $a$ satisfy $1 \\leq a \\leq s - a \\leq M$. We can derive four bounds on $a$ from this.\n",
"* $1 \\leq a$\n",
"* $s - M \\leq a$\n",
"* $a \\leq \\frac{s}{2}$\n",
"* $a \\leq M$\n",
"\n",
"From these bounds, we can get the number of cuboids that can be constructed from an $(s, M)$ pair."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6ce96a6e",
"metadata": {},
"outputs": [],
"source": [
"def leg_splits(s, M):\n",
" max_a = min(M, s // 2 + 1)\n",
" min_a = max(s - M, 1)\n",
" return max_a - min_a"
]
},
{
"cell_type": "markdown",
"id": "00e62dfc",
"metadata": {},
"source": [
"Then we can write a function to find the number of cuboids with at least one edge equaling $M$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "caf59499",
"metadata": {},
"outputs": [],
"source": [
"def count_cuboids(M):\n",
" return sum(leg_splits(s, M) for s in range(1, 2 * M + 1) if is_square(s^2 + M^2))"
]
},
{
"cell_type": "markdown",
"id": "d110565c",
"metadata": {},
"source": [
"To get our answer, we just compute a running total and stop when it exceeds one million."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "984b7665",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1818"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import count\n",
"\n",
"total = 0\n",
"for M in count(1):\n",
" total += count_cuboids(M)\n",
" if total > 1000000:\n",
" break\n",
" \n",
"M"
]
},
{
"cell_type": "markdown",
"id": "488ec2af",
"metadata": {},
"source": [
"## Relevant sequences\n",
"* Number of pairs $a,b$ such that $(a+b)^2 + n^2$ is square: [A143714](https://oeis.org/A143714)\n",
"* Partial sums of A143714: [A143715](https://oeis.org/A143715)\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
}