|
| 1 | +# [Problem 1925: Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I need to count integer triples (a, b, c) with 1 <= a, b, c <= n and a^2 + b^2 = c^2. The domain n is up to 250, which is small enough to allow an O(n^2) enumeration. One straightforward idea: for each c and each a (or for each pair a, b) check whether c^2 - a^2 is a perfect square (that's b^2). Using integer square root (math.isqrt) avoids floating-point issues. The problem counts ordered pairs, since examples include both (3,4,5) and (4,3,5). |
| 5 | + |
| 6 | +## Refining the problem, round 2 thoughts |
| 7 | +- We should ensure we count ordered (a,b) pairs. If we iterate a and b both from 1..n and check if a^2 + b^2 is a perfect square <= n^2, that directly counts ordered pairs. |
| 8 | +- Slightly fewer checks if we iterate c from 1..n and a from 1..c-1 (since a,b < c when a^2 + b^2 = c^2 with positive integers), compute b^2 = c^2 - a^2 and check with isqrt. This still counts ordered pairs because when a takes the other value, we'll count the swapped pair too. |
| 9 | +- Use math.isqrt to test perfect square safely. |
| 10 | +- Complexity: O(n^2) time, O(1) extra space. For n <= 250 this is trivial to run. |
| 11 | +- An alternative: generate Pythagorean triples using Euclid's formula and scale them, which can be more efficient for larger n, but unnecessary here. |
| 12 | + |
| 13 | +## Attempted solution(s) |
| 14 | +```python |
| 15 | +import math |
| 16 | +from typing import * |
| 17 | + |
| 18 | +class Solution: |
| 19 | + def countTriples(self, n: int) -> int: |
| 20 | + count = 0 |
| 21 | + # Iterate c and a, compute b via integer sqrt |
| 22 | + for c in range(1, n + 1): |
| 23 | + c2 = c * c |
| 24 | + # a and b must be positive and less than c for a^2 + b^2 = c^2 |
| 25 | + for a in range(1, c): |
| 26 | + diff = c2 - a * a |
| 27 | + b = math.isqrt(diff) |
| 28 | + if b >= 1 and b < c and b * b == diff: |
| 29 | + count += 1 |
| 30 | + return count |
| 31 | +``` |
| 32 | +- Notes: |
| 33 | + - Approach: iterate c from 1..n, for each a in 1..c-1 compute b = isqrt(c^2 - a^2) and check b^2 equals the difference and b < c. Each valid (a,b,c) found is counted; symmetry ensures (b,a,c) will be counted when a iterates to the other value, so ordered pairs are accounted for. |
| 34 | + - Time complexity: O(n^2) iterations; each iteration does O(1) work (integer arithmetic and isqrt) — overall O(n^2). |
| 35 | + - Space complexity: O(1) extra space. |
| 36 | + - math.isqrt avoids floating-point rounding errors and is efficient. |
| 37 | + - Given constraint n <= 250 this solution is more than fast enough. |
0 commit comments