Right Triangles with Integer Coordinates¶

We'll start by generating all the possible values of $P$ and $Q$.

In [1]:
limit = 50
points = ((x, y) for x in range(0, limit + 1) for y in range(0, limit + 1) if (x, y) != (0, 0))

If we think about $P$ and $Q$ as vectors instead of points, we can solve this problem with dot products. 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}$.

In [2]:
from itertools import combinations

triangles = set()
for ((x1, y1), (x2, y2)) in combinations(points, 2):
    d = x1 * x2 + y1 * y2
    if d == 0 or d == x1^2 + y1^2 or d == x2^2 + y2^2:
        triangles.add(((x1, y1), (x2, y2)))
        
len(triangles)
Out[2]:
14234

Relevant sequences¶

  • Answers for limits of 0, 1, 2, ...: A155154

Copyright (C) 2025 filifa¶

This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.