Count Square Sum Triples — LeetCode 1925 Python Solution
- Problem
- #1925
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
The problem
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
- Input
- n = 5
- Output
- 2
- Explanation
- The square triples are (3,4,5) and (4,3,5).
Python solution
class Solution:
def countTriples(self, n: int) -> int:
ans = 0
for a in range(1, n):
for b in range(1, n):
x = a * a + b * b
c = int(sqrt(x))
if c <= n and c * c == x:
ans += 1
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n^2), where n is the given integer |
| Space | O(1) auxiliary |
Pattern: Math and Number Theory
Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1925. Count Square Sum Triples is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math.
The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1925. Count Square Sum Triples?
- LeetCode 1925. Count Square Sum Triples is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1925. Count Square Sum Triples?
- The Python solution on this page runs in O(n^2), where n is the given integer.
- What is the space complexity of LeetCode 1925. Count Square Sum Triples?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1925. Count Square Sum Triples cover?
- LeetCode 1925. Count Square Sum Triples is tagged Math and Enumeration on LeetCode.