Count Square Sum Triples - Problem

A square triple (a,b,c) is a triple where a, b, and c are integers and a² + b² = c².

Given an integer n, return the number of square triples such that 1 ≤ a, b, c ≤ n.

For example, if n = 5, the square triples are (3,4,5) and (4,3,5), so the answer is 2.

Input & Output

Example 1 — Basic Case
$ Input: n = 5
Output: 2
💡 Note: The square triples are (3,4,5) and (4,3,5). Both satisfy 3² + 4² = 9 + 16 = 25 = 5² and all values are ≤ 5.
Example 2 — Small Range
$ Input: n = 3
Output: 0
💡 Note: No valid square triples exist with all values ≤ 3. The smallest Pythagorean triple is (3,4,5) but 4 and 5 exceed the limit.
Example 3 — Larger Range
$ Input: n = 10
Output: 4
💡 Note: Valid triples: (3,4,5), (4,3,5), (6,8,10), (8,6,10). Each satisfies the Pythagorean theorem with all values ≤ 10.

Constraints

  • 1 ≤ n ≤ 250

Visualization

Tap to expand
Count Square Sum Triples: n = 5Input Range1 ≤ a,b,c ≤ 5Pythagorean Checka² + b² = c²Valid TriplesCount = 2Found Triples:(3, 4, 5)3² + 4² = 9 + 16 = 25 = 5²(4, 3, 5)4² + 3² = 16 + 9 = 25 = 5²Answer: 2Both (a,b,c) orderings count as separate triples
Understanding the Visualization
1
Input
Given n=5, find all (a,b,c) where a²+b²=c² and 1≤a,b,c≤5
2
Process
Check all combinations for Pythagorean relationship
3
Output
Count of valid triples: 2
Key Takeaway
🎯 Key Insight: Use the Pythagorean theorem to identify valid right triangle integer sides
Asked in
Google 25 Microsoft 20 Apple 15
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen