diff --git a/solution/1900-1999/1925.Count Square Sum Triples/README.md b/solution/1900-1999/1925.Count Square Sum Triples/README.md index 5433a1a501c94..c145fa9b51ce3 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/README.md +++ b/solution/1900-1999/1925.Count Square Sum Triples/README.md @@ -53,7 +53,13 @@ tags: -### 方法一 +### 方法一:枚举 + +我们在 $[1, n)$ 的范围内枚举 $a$ 和 $b$,然后计算 $c = \sqrt{a^2 + b^2}$,如果 $c$ 是整数且 $c \leq n$,那么就找到了一个平方和三元组,答案加一。 + +枚举结束后,返回答案即可。 + +时间复杂度 $O(n^2)$,其中 $n$ 是给定的整数。空间复杂度 $O(1)$。 @@ -62,14 +68,14 @@ tags: ```python class Solution: def countTriples(self, n: int) -> int: - res = 0 - for a in range(1, n + 1): - for b in range(1, n + 1): - t = a**2 + b**2 - c = int(sqrt(t)) - if c <= n and c**2 == t: - res += 1 - return res + 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 ans ``` #### Java @@ -77,17 +83,17 @@ class Solution: ```java class Solution { public int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) Math.sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; a++) { + for (int b = 1; b < n; b++) { + int x = a * a + b * b; + int c = (int) Math.sqrt(x); + if (c <= n && c * c == x) { + ans++; } } } - return res; + return ans; } } ``` @@ -98,17 +104,17 @@ class Solution { class Solution { public: int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; ++a) { + for (int b = 1; b < n; ++b) { + int x = a * a + b * b; + int c = static_cast(sqrt(x)); + if (c <= n && c * c == x) { + ++ans; } } } - return res; + return ans; } }; ``` @@ -116,18 +122,35 @@ public: #### Go ```go -func countTriples(n int) int { - res := 0 - for a := 1; a <= n; a++ { - for b := 1; b <= n; b++ { - t := a*a + b*b - c := int(math.Sqrt(float64(t))) - if c <= n && c*c == t { - res++ +func countTriples(n int) (ans int) { + for a := 1; a < n; a++ { + for b := 1; b < n; b++ { + x := a*a + b*b + c := int(math.Sqrt(float64(x))) + if c <= n && c*c == x { + ans++ } } } - return res + return +} +``` + +#### TypeScript + +```ts +function countTriples(n: number): number { + let ans = 0; + for (let a = 1; a < n; a++) { + for (let b = 1; b < n; b++) { + const x = a * a + b * b; + const c = Math.floor(Math.sqrt(x)); + if (c <= n && c * c === x) { + ans++; + } + } + } + return ans; } ``` diff --git a/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md b/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md index a9e377946bc4b..11fa1f0b37bc2 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md +++ b/solution/1900-1999/1925.Count Square Sum Triples/README_EN.md @@ -53,7 +53,13 @@ tags: -### Solution 1 +### Solution 1: Enumeration + +We enumerate $a$ and $b$ in the range $[1, n)$, then calculate $c = \sqrt{a^2 + b^2}$. If $c$ is an integer and $c \leq n$, then we have found a Pythagorean triplet, and we increment the answer by one. + +After the enumeration is complete, return the answer. + +The time complexity is $O(n^2)$, where $n$ is the given integer. The space complexity is $O(1)$. @@ -62,14 +68,14 @@ tags: ```python class Solution: def countTriples(self, n: int) -> int: - res = 0 - for a in range(1, n + 1): - for b in range(1, n + 1): - t = a**2 + b**2 - c = int(sqrt(t)) - if c <= n and c**2 == t: - res += 1 - return res + 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 ans ``` #### Java @@ -77,17 +83,17 @@ class Solution: ```java class Solution { public int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) Math.sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; a++) { + for (int b = 1; b < n; b++) { + int x = a * a + b * b; + int c = (int) Math.sqrt(x); + if (c <= n && c * c == x) { + ans++; } } } - return res; + return ans; } } ``` @@ -98,17 +104,17 @@ class Solution { class Solution { public: int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; ++a) { + for (int b = 1; b < n; ++b) { + int x = a * a + b * b; + int c = static_cast(sqrt(x)); + if (c <= n && c * c == x) { + ++ans; } } } - return res; + return ans; } }; ``` @@ -116,18 +122,35 @@ public: #### Go ```go -func countTriples(n int) int { - res := 0 - for a := 1; a <= n; a++ { - for b := 1; b <= n; b++ { - t := a*a + b*b - c := int(math.Sqrt(float64(t))) - if c <= n && c*c == t { - res++ +func countTriples(n int) (ans int) { + for a := 1; a < n; a++ { + for b := 1; b < n; b++ { + x := a*a + b*b + c := int(math.Sqrt(float64(x))) + if c <= n && c*c == x { + ans++ } } } - return res + return +} +``` + +#### TypeScript + +```ts +function countTriples(n: number): number { + let ans = 0; + for (let a = 1; a < n; a++) { + for (let b = 1; b < n; b++) { + const x = a * a + b * b; + const c = Math.floor(Math.sqrt(x)); + if (c <= n && c * c === x) { + ans++; + } + } + } + return ans; } ``` diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp index 89e0a390ffca4..61217b77a5cd1 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp @@ -1,16 +1,16 @@ class Solution { public: int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; ++a) { + for (int b = 1; b < n; ++b) { + int x = a * a + b * b; + int c = static_cast(sqrt(x)); + if (c <= n && c * c == x) { + ++ans; } } } - return res; + return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.go b/solution/1900-1999/1925.Count Square Sum Triples/Solution.go index 855674718cfe2..87bf2c7dc4175 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/Solution.go +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.go @@ -1,13 +1,12 @@ -func countTriples(n int) int { - res := 0 - for a := 1; a <= n; a++ { - for b := 1; b <= n; b++ { - t := a*a + b*b - c := int(math.Sqrt(float64(t))) - if c <= n && c*c == t { - res++ +func countTriples(n int) (ans int) { + for a := 1; a < n; a++ { + for b := 1; b < n; b++ { + x := a*a + b*b + c := int(math.Sqrt(float64(x))) + if c <= n && c*c == x { + ans++ } } } - return res -} \ No newline at end of file + return +} diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.java b/solution/1900-1999/1925.Count Square Sum Triples/Solution.java index 134a540237133..184a0e0274a25 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/Solution.java +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.java @@ -1,15 +1,15 @@ class Solution { public int countTriples(int n) { - int res = 0; - for (int a = 1; a <= n; ++a) { - for (int b = 1; b <= n; ++b) { - int t = a * a + b * b; - int c = (int) Math.sqrt(t); - if (c <= n && c * c == t) { - ++res; + int ans = 0; + for (int a = 1; a < n; a++) { + for (int b = 1; b < n; b++) { + int x = a * a + b * b; + int c = (int) Math.sqrt(x); + if (c <= n && c * c == x) { + ans++; } } } - return res; + return ans; } -} \ No newline at end of file +} diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.py b/solution/1900-1999/1925.Count Square Sum Triples/Solution.py index 062387766b643..ee8894911aa95 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/Solution.py +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.py @@ -1,10 +1,10 @@ class Solution: def countTriples(self, n: int) -> int: - res = 0 - for a in range(1, n + 1): - for b in range(1, n + 1): - t = a**2 + b**2 - c = int(sqrt(t)) - if c <= n and c**2 == t: - res += 1 - return res + 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 ans diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.ts b/solution/1900-1999/1925.Count Square Sum Triples/Solution.ts new file mode 100644 index 0000000000000..ee4b06377e26a --- /dev/null +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.ts @@ -0,0 +1,13 @@ +function countTriples(n: number): number { + let ans = 0; + for (let a = 1; a < n; a++) { + for (let b = 1; b < n; b++) { + const x = a * a + b * b; + const c = Math.floor(Math.sqrt(x)); + if (c <= n && c * c === x) { + ans++; + } + } + } + return ans; +}