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
.
Example 1:
Input: n = 5 Output: 2 Explanation: The square triples are (3,4,5) and (4,3,5).
Example 2:
Input: n = 10 Output: 4 Explanation: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10).
Constraints:
1 <= n <= 250
struct Solution;
impl Solution {
fn count_triples(n: i32) -> i32 {
let mut res = 0;
for a in 1..n {
for b in a + 1..n {
for c in b + 1..=n {
if a * a + b * b == c * c {
res += 1;
}
}
}
}
2 * res
}
}
#[test]
fn test() {
let n = 5;
let res = 2;
assert_eq!(Solution::count_triples(n), res);
let n = 10;
let res = 4;
assert_eq!(Solution::count_triples(n), res);
}