#1925
Easy Algorithms Count square sum triples
Math Enumeration
77.1% acceptance
Feb 25, 2026
736
64
A square triple (a,b,c) is a triple where a, b, and c are integers and a2 + b2 = c2.
Given an integer n, return the number of square triples such that 1 <= a, b, c <= n.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn count_triples(n: i32) -> i32 {
let mut count = 0;
for a in 1..=n {
for b in 1..=n {
let c2 = a * a + b * b;
let c = (c2 as f64).sqrt() as i32;
if c <= n && c * c == c2 {
count += 1;
}
}
}
count
}
}