Ctrl K

Whole-Number Right Triangles

Easy
A carpenter builds right triangles from slats with whole-number lengths from 1 to limit. A triple (a, b, c) works if a² + b² = c²: a and b are the legs and c is the hypotenuse. Return how many such triples have all three numbers between 1 and limit. The order of the legs matters: (3, 4, 5) and (4, 3, 5) are two different triples.
Example 1
limit=5 → 2

Only the 3-4-5 triangle fits, and its legs can be listed in two orders: (3, 4, 5) and (4, 3, 5).

Example 2
limit=13 → 6

The triangles 3-4-5, 6-8-10 and 5-12-13, each with two orders of the legs, give 6.

Example 3
limit=4 → 0

The smallest triangle needs a hypotenuse of 5, which exceeds limit.

Constraints
  • 1 ≤ limit ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest Short Hex Color EasyXOR of Every Subset EasyA Trio of Divisors EasyCount Shared Divisors EasyEqual Half Digit Sums Easy
loading Python…
Input
limit=5
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 26 tests on the server, hidden ones included.