173 lines
4.4 KiB
Plaintext
173 lines
4.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "78c9390b",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Integer Right Triangles](https://projecteuler.net/problem=39)\n",
|
|
"\n",
|
|
"If a right triangle has integer side lengths, the side lengths are a [Pythagorean triple](https://en.wikipedia.org/wiki/Pythagorean_triple). In [problem 9](https://projecteuler.net/problem=9), we wrote a generator for primitive Pythagorean triples based off of Euclid's formula. We can modify that generator to cut off after the triplets have passed a maximum perimeter. Note that a triangle with side lengths generated by Euclid's formula will have perimeter $2m^2 + 2mn$."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "b03eb872",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from itertools import count\n",
|
|
"\n",
|
|
"def primitive_pythagorean_triplets(max_perim):\n",
|
|
" for m in count(2):\n",
|
|
" if 2*m^2 + 2*m > max_perim:\n",
|
|
" break\n",
|
|
"\n",
|
|
" for n in range(1, m):\n",
|
|
" if not ((m % 2) != (n % 2)) or gcd(m, n) != 1:\n",
|
|
" continue\n",
|
|
" \n",
|
|
" a = m^2 - n^2\n",
|
|
" b = 2*m*n\n",
|
|
" c = m^2 + n^2\n",
|
|
" \n",
|
|
" if a + b + c > max_perim:\n",
|
|
" break\n",
|
|
" \n",
|
|
" yield (a, b, c)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "431ef21c",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now we can just iterate through our new generator and group each triangle by their perimeters. We also multiply to consider non-primitive triplets."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "62aa955f",
|
|
"metadata": {
|
|
"scrolled": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"max_perim = 1000\n",
|
|
"perimeters = dict()\n",
|
|
"for (a, b, c) in primitive_pythagorean_triplets(max_perim):\n",
|
|
" for k in count(1):\n",
|
|
" perimeter = k * (a + b + c)\n",
|
|
" if perimeter > max_perim:\n",
|
|
" break\n",
|
|
" \n",
|
|
" if perimeter not in perimeters:\n",
|
|
" perimeters[perimeter] = set()\n",
|
|
" perimeters[perimeter].add((k*a, k*b, k*c))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f776a5c6",
|
|
"metadata": {},
|
|
"source": [
|
|
"Our answer is whichever perimeter has the highest total."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"id": "0cd6241f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"840"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"p = max(perimeters, key=lambda x: len(perimeters[x]))\n",
|
|
"p"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "f32fb164",
|
|
"metadata": {},
|
|
"source": [
|
|
"There are eight right triangles with this perimeter."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"id": "9c31c68f",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{(105, 360, 375),\n",
|
|
" (140, 336, 364),\n",
|
|
" (210, 280, 350),\n",
|
|
" (252, 240, 348),\n",
|
|
" (315, 168, 357),\n",
|
|
" (350, 120, 370),\n",
|
|
" (390, 56, 394),\n",
|
|
" (399, 40, 401)}"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"perimeters[p]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6cb1b692",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Related sequences\n",
|
|
"* Number of integer right triangles with perimeter $n$: [A024155](https://oeis.org/A024155)\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
|
|
}
|