Problem Description
Given an integer n, count the number of square triples (a, b, c) such that 1 <= a, b, c <= n and a² + b² = c². Note that different orderings of a and b count as distinct triples.
Key Insights
- Use brute-force enumeration since n is small (n <= 250).
- Iterate a and b in the range [1, n] and compute a² + b².
- Check if the computed sum is a perfect square.
- Ensure that the square root (c) is within the range [1, n].
- Each valid triple (a, b, c) is counted separately even if a and b are swapped.
Space and Time Complexity
Time Complexity: O(n²) because of the nested iterations over a and b. Space Complexity: O(1) since only a few variables are used.
Solution
The solution involves iterating over all possible pairs (a, b) from 1 to n. For each pair, we calculate the sum of squares a² + b² and check if its square root is an integer using a square root function. If it is, and if the resulting c (the integer square root) is within the allowed range (1 to n), then we count that as a valid square triple. This approach uses double nested loops and basic arithmetic, making it both simple and effective for the given constraints.