Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 8, 2025 19:24
Show Gist options
  • Select an option

  • Save Ifihan/de91de2392e67956f44b2f0c18517d19 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/de91de2392e67956f44b2f0c18517d19 to your computer and use it in GitHub Desktop.
Count Square Sum Triples

Question

Approach

I iterate over all ordered pairs (a,b) with 1 ≤ a,b ≤ n and compute s = aa + bb. I take the integer square root c = isqrt(s) and check whether c*c == s and c ≤ n. If so, (a,b,c) is a valid square triple — I count it.

Implementation

class Solution:
    def countTriples(self, n: int) -> int:
        ans = 0
        for a in range(1, n + 1):
            for b in range(1, n + 1):
                s = a*a + b*b
                c = math.isqrt(s)
                if c*c == s and c <= n:
                    ans += 1
        return ans

Complexities

  • Time: O(n^2)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment