|
| 1 | +# [1925. Count Square Sum Triples](https://leetcode.com/problems/count-square-sum-triples) |
| 2 | + |
| 3 | +[中文文档](/solution/1900-1999/1925.Count%20Square%20Sum%20Triples/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>A <strong>square triple</strong> <code>(a,b,c)</code> is a triple where <code>a</code>, <code>b</code>, and <code>c</code> are <strong>integers</strong> and <code>a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup></code>.</p> |
| 8 | + |
| 9 | +<p>Given an integer <code>n</code>, return <em>the number of <strong>square triples</strong> such that </em><code>1 <= a, b, c <= n</code>.</p> |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong>Example 1:</strong></p> |
| 13 | + |
| 14 | +<pre> |
| 15 | +<strong>Input:</strong> n = 5 |
| 16 | +<strong>Output:</strong> 2 |
| 17 | +<strong>Explanation</strong>: The square triples are (3,4,5) and (4,3,5). |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong>Example 2:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<strong>Input:</strong> n = 10 |
| 24 | +<strong>Output:</strong> 4 |
| 25 | +<strong>Explanation</strong>: The square triples are (3,4,5), (4,3,5), (6,8,10), and (8,6,10). |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p> </p> |
| 29 | +<p><strong>Constraints:</strong></p> |
| 30 | + |
| 31 | +<ul> |
| 32 | + <li><code>1 <= n <= 250</code></li> |
| 33 | +</ul> |
| 34 | + |
| 35 | + |
| 36 | +## Solutions |
| 37 | + |
| 38 | +<!-- tabs:start --> |
| 39 | + |
| 40 | +### **Python3** |
| 41 | + |
| 42 | +```python |
| 43 | +class Solution: |
| 44 | + def countTriples(self, n: int) -> int: |
| 45 | + res = 0 |
| 46 | + for a in range(1, n + 1): |
| 47 | + for b in range(1, n + 1): |
| 48 | + t = a ** 2 + b ** 2 |
| 49 | + c = int(sqrt(t)) |
| 50 | + if c <= n and c ** 2 == t: |
| 51 | + res += 1 |
| 52 | + return res |
| 53 | +``` |
| 54 | + |
| 55 | +### **Java** |
| 56 | + |
| 57 | +```java |
| 58 | +class Solution { |
| 59 | + public int countTriples(int n) { |
| 60 | + int res = 0; |
| 61 | + for (int a = 1; a <= n; ++a) { |
| 62 | + for (int b = 1; b <= n; ++b) { |
| 63 | + int t = a * a + b * b; |
| 64 | + int c = (int) Math.sqrt(t); |
| 65 | + if (c <= n && c * c == t) { |
| 66 | + ++res; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return res; |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### **C++** |
| 76 | + |
| 77 | +```cpp |
| 78 | +class Solution { |
| 79 | +public: |
| 80 | + int countTriples(int n) { |
| 81 | + int res = 0; |
| 82 | + for (int a = 1; a <= n; ++a) { |
| 83 | + for (int b = 1; b <= n; ++b) { |
| 84 | + int t = a * a + b * b; |
| 85 | + int c = (int) sqrt(t); |
| 86 | + if (c <= n && c * c == t) { |
| 87 | + ++res; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return res; |
| 92 | + } |
| 93 | +}; |
| 94 | +``` |
| 95 | +
|
| 96 | +### **Go** |
| 97 | +
|
| 98 | +```go |
| 99 | +func countTriples(n int) int { |
| 100 | + res := 0 |
| 101 | + for a := 1; a <= n; a++ { |
| 102 | + for b := 1; b <= n; b++ { |
| 103 | + t := a*a + b*b |
| 104 | + c := int(math.Sqrt(float64(t))) |
| 105 | + if c <= n && c*c == t { |
| 106 | + res++ |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return res |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +### **...** |
| 115 | + |
| 116 | +``` |
| 117 | +
|
| 118 | +``` |
| 119 | + |
| 120 | +<!-- tabs:end --> |
0 commit comments