Classic problem #1925EasyMathEnumeration
Count Square Sum Triples
Real-world version: Triples of Squares That Add UpTracked in this browser only
Read Count Square Sum Triples on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
Return how many ordered triples (a, b, c) of whole numbers between 1 and n satisfy a * a + b * b == c * c.
Triples in a different order count separately, so (3, 4, 5) and (4, 3, 5) are two of them.
Examples
Example 1
- Input
- n = 13
- Output
- 6
Three pairs of legs work within thirteen, each counted twice for its two orders: 3 with 4, 6 with 8, and 5 with 12.
Example 2
- Input
- n = 12
- Output
- 4
Two pairs work within twelve, 3 with 4 and 6 with 8, each counted twice.
Example 3
- Input
- n = 4
- Output
- 0
Nothing within four works at all.
Constraints
- 1 <= n <= 250
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def count_triples(n: int) -> int:Java
public int countTriples(int n)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Smallest Divisible Digit Product IReal-world version: Next Shelf LabelEasy
- Count PrimesReal-world version: Indivisible Catalogue NumbersMedium
- Total Waviness of Numbers in Range IReal-world version: Serial Number KinksMedium
- Reordered Power of 2Real-world version: Rearranging the Digits Into a Power of TwoMedium