"# [Right Triangles with Integer Coordinates](https://projecteuler.net/problem=91)\n",
"\n",
"We'll start by generating all the possible values of $P$ and $Q$."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cba05ca7",
"metadata": {},
"outputs": [],
"source": [
"limit = 50\n",
"points = ((x, y) for x in range(0, limit + 1) for y in range(0, limit + 1) if (x, y) != (0, 0))"
]
},
{
"cell_type": "markdown",
"id": "90cb43dd",
"metadata": {},
"source": [
"If we think about $P$ and $Q$ as vectors instead of points, we can solve this problem with [dot products](https://en.wikipedia.org/wiki/Dot_product). Since the dot product of orthogonal vectors is 0, we can check for a right angle in the triangle by seeing if $\\vec{P} \\cdot \\vec{Q} = 0$, $\\vec{P} \\cdot (\\vec{Q} - \\vec{P}) = 0$, or $\\vec{Q} \\cdot (\\vec{Q} - \\vec{P}) = 0$. By distributing in the last two equations, we can simply check if $\\vec{P} \\cdot \\vec{Q}$ equals 0, $\\vec{P} \\cdot \\vec{P}$, or $\\vec{Q} \\cdot \\vec{Q}$."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "18e334eb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14234"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import combinations\n",
"\n",
"triangles = set()\n",
"for ((x1, y1), (x2, y2)) in combinations(points, 2):\n",
" d = x1 * x2 + y1 * y2\n",
" if d == 0 or d == x1^2 + y1^2 or d == x2^2 + y2^2:\n",
"* Answers for limits of 0, 1, 2, ...: [A155154](https://oeis.org/A155154)\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)."