Editor

Right Triangles With Whole Sides

#323 · Math · Intermediate · 75 XP · about 15 min

A carpenter checks square corners with a 3-4-5 triangle: 3 * 3 + 4 * 4 = 5 * 5, so the corner between the sides 3 and 4 is a right angle. Other whole-number triangles work too, such as 5-12-13 and 6-8-10.

Write right_triangles(p) that returns every right triangle with whole-number sides and a perimeter of at most p, as a list of tuples (a, b, c) with a < b < c and a * a + b * b == c * c. Sort the list by a, then by b (the natural order of the tuples).

Examples

right_triangles(12)    # returns [(3, 4, 5)]
right_triangles(30)    # returns [(3, 4, 5), (5, 12, 13), (6, 8, 10)]
right_triangles(11)    # returns []

Constraints

  • 1 <= p <= 2,000
  • Scaled copies of a triangle, such as (6, 8, 10), count too.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.