Count Square Sum Triples

easy math enumeration

Problem

A square triple is a group of three positive integers (a, b, c) such that a² + b² = c². Given an integer n, return the number of square triples whose three values are each at least 1 and at most n. The triples are ordered, so (a, b, c) and (b, a, c) count separately.

Inputn = 5
Output2
The square triples are (3, 4, 5) and (4, 3, 5): both give 9 + 16 = 25 = 5², and every value is between 1 and 5.

def count_triples(n):
    count = 0
    for a in range(1, n + 1):
        for b in range(1, n + 1):
            s = a * a + b * b
            c = int(s ** 0.5)
            if c <= n and c * c == s:
                count += 1
    return count
function countTriples(n) {
  let count = 0;
  for (let a = 1; a <= n; a++) {
    for (let b = 1; b <= n; b++) {
      const s = a * a + b * b;
      const c = Math.floor(Math.sqrt(s));
      if (c <= n && c * c === s) count++;
    }
  }
  return count;
}
class Solution {
    public int countTriples(int n) {
        int count = 0;
        for (int a = 1; a <= n; a++) {
            for (int b = 1; b <= n; b++) {
                int s = a * a + b * b;
                int c = (int) Math.sqrt(s);
                if (c <= n && c * c == s) count++;
            }
        }
        return count;
    }
}
int countTriples(int n) {
    int count = 0;
    for (int a = 1; a <= n; a++) {
        for (int b = 1; b <= n; b++) {
            int s = a * a + b * b;
            int c = (int) sqrt((double) s);
            if (c <= n && c * c == s) count++;
        }
    }
    return count;
}
Time: O(n²) Space: O(1)