Skip to content

Right Triangles with Integer Coordinates (91)

In Problem 91, we're asked to find right triangles on a grid of integer coordinates.

Brute force

The coordinates in the grid extend from 0 to 51 for both x and y, one point is fixed at the origin. Hence we have 514=6765201. If we can make the check fast enough, we can just brute force this.

The check whether something is a right triangle is rather easy: Compute the three side lengths squared:

s12:=x12+y12,s22:=x22+y22,s32:=(x1x2)2+(y1y2)2.

Then find the longest side length and call it h, call the other side lengths a and o. One has a right triangle if h2=a2+o2.

One has to be a bit careful not to count triangles twice when (x1,y1) and (x2,y2) are exchanged.

My Rust implementation runs in 39 ms, so clearly fast enough. Perhaps there is a more clever way to this, but one doesn't need to.