From 0fcba4a2e6557df0456aef7d57529dd20ae69fd0 Mon Sep 17 00:00:00 2001 From: filifa Date: Tue, 17 Jun 2025 23:08:40 -0400 Subject: [PATCH] add problem 91 --- notebooks/problem0091.ipynb | 92 +++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 notebooks/problem0091.ipynb diff --git a/notebooks/problem0091.ipynb b/notebooks/problem0091.ipynb new file mode 100644 index 0000000..4c2aeaa --- /dev/null +++ b/notebooks/problem0091.ipynb @@ -0,0 +1,92 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "dcfe1d77", + "metadata": {}, + "source": [ + "# [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", + " triangles.add(((x1, y1), (x2, y2)))\n", + " \n", + "len(triangles)" + ] + }, + { + "cell_type": "markdown", + "id": "35a4a234", + "metadata": {}, + "source": [ + "## Relevant sequences\n", + "* Answers for limits of 0, 1, 2, ...: [A155154](https://oeis.org/A155154)" + ] + } + ], + "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 +}