Skip to content

Commit 534741b

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.0633 (#3118)
* feat: add 2nd ts solution to lc problem: No.0633 * feat: add 3rd ts solution to lc problem: No.0633 * Update README.md * style: format code and docs with prettier * Update README_EN.md * Update Solution2.ts * Create Solution2.py * Create Solution2.java * Create Solution2.cpp * Create Solution2.go * style: format code and docs with prettier --------- Co-authored-by: Libin YANG <contact@yanglibin.info>
1 parent 12fd30b commit 534741b

File tree

7 files changed

+348
-0
lines changed

7 files changed

+348
-0
lines changed

solution/0600-0699/0633.Sum of Square Numbers/README.md

+134
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,138 @@ impl Solution {
193193

194194
<!-- solution:end -->
195195

196+
<!-- solution:start -->
197+
198+
### 方法二:数学
199+
200+
这个问题实际上是关于一个数能否表示为两个平方数之和的条件。这个定理可以追溯到费马(Fermat)和欧拉(Euler),它在数论中是一个经典结果。
201+
202+
具体来说,这个定理可以表述为:
203+
204+
**一个正整数 \( n \) 能表示为两个平方数之和的充要条件是:\( n \) 的所有形如 \( 4k + 3 \) 的素数因子的幂次均为偶数。**
205+
206+
这意味着,如果我们将 $n$ 分解成素数因子乘积的形式,即 $n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}$,其中 $p_i$ 是素数且 $e_i$ 是它们对应的幂次,那么 $n$ 可以表示为两个平方数之和,当且仅当所有 $4k + 3$ 形式的素数因子 $p_i$ 的幂次 $e_i$ 都是偶数。
207+
208+
更正式地,假设 $p_i$ 是形如 $4k + 3$ 的素数,则对于每一个这样的 $p_i$,要求 $e_i$ 是偶数。
209+
210+
例如:
211+
212+
- 数字 $13$ 是素数,且 $13 \equiv 1 \pmod{4}$,因此它可以表示为两个平方数之和,即 $13 = 2^2 + 3^2$。
213+
- 数字 $21$ 分解为 $3 \times 7$,其中 $3$ 和 $7$ 都是形如 $4k + 3$ 的素数因子,并且它们的幂次都是 $1$(奇数),因此 $21$ 不能表示为两个平方数之和。
214+
215+
总结起来,这个定理在数论中非常重要,用于判断一个数是否可以表示为两个平方数之和。
216+
217+
时间复杂度 $O(\sqrt{c})$,其中 $c$ 是给定的非负整数。空间复杂度 $O(1)$。
218+
219+
<!-- tabs:start -->
220+
221+
#### Python3
222+
223+
```python
224+
class Solution:
225+
def judgeSquareSum(self, c: int) -> bool:
226+
for i in range(2, int(sqrt(c)) + 1):
227+
if c % i == 0:
228+
exp = 0
229+
while c % i == 0:
230+
c //= i
231+
exp += 1
232+
if i % 4 == 3 and exp % 2 != 0:
233+
return False
234+
return c % 4 != 3
235+
```
236+
237+
#### Java
238+
239+
```java
240+
class Solution {
241+
public boolean judgeSquareSum(int c) {
242+
int n = (int) Math.sqrt(c);
243+
for (int i = 2; i <= n; ++i) {
244+
if (c % i == 0) {
245+
int exp = 0;
246+
while (c % i == 0) {
247+
c /= i;
248+
++exp;
249+
}
250+
if (i % 4 == 3 && exp % 2 != 0) {
251+
return false;
252+
}
253+
}
254+
}
255+
return c % 4 != 3;
256+
}
257+
}
258+
```
259+
260+
#### C++
261+
262+
```cpp
263+
class Solution {
264+
public:
265+
bool judgeSquareSum(int c) {
266+
int n = sqrt(c);
267+
for (int i = 2; i <= n; ++i) {
268+
if (c % i == 0) {
269+
int exp = 0;
270+
while (c % i == 0) {
271+
c /= i;
272+
++exp;
273+
}
274+
if (i % 4 == 3 && exp % 2 != 0) {
275+
return false;
276+
}
277+
}
278+
}
279+
return c % 4 != 3;
280+
}
281+
};
282+
```
283+
284+
#### Go
285+
286+
```go
287+
func judgeSquareSum(c int) bool {
288+
n := int(math.Sqrt(float64(c)))
289+
for i := 2; i <= n; i++ {
290+
if c%i == 0 {
291+
exp := 0
292+
for c%i == 0 {
293+
c /= i
294+
exp++
295+
}
296+
if i%4 == 3 && exp%2 != 0 {
297+
return false
298+
}
299+
}
300+
}
301+
return c%4 != 3
302+
}
303+
```
304+
305+
#### TypeScript
306+
307+
```ts
308+
function judgeSquareSum(c: number): boolean {
309+
const n = Math.floor(Math.sqrt(c));
310+
for (let i = 2; i <= n; ++i) {
311+
if (c % i === 0) {
312+
let exp = 0;
313+
while (c % i === 0) {
314+
c /= i;
315+
++exp;
316+
}
317+
if (i % 4 === 3 && exp % 2 !== 0) {
318+
return false;
319+
}
320+
}
321+
}
322+
return c % 4 !== 3;
323+
}
324+
```
325+
326+
<!-- tabs:end -->
327+
328+
<!-- solution:end -->
329+
196330
<!-- problem:end -->

solution/0600-0699/0633.Sum of Square Numbers/README_EN.md

+134
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,138 @@ impl Solution {
191191

192192
<!-- solution:end -->
193193

194+
<!-- solution:start -->
195+
196+
### Solution 2: Mathematics
197+
198+
This problem is essentially about the conditions under which a number can be expressed as the sum of two squares. This theorem dates back to Fermat and Euler and is a classic result in number theory.
199+
200+
Specifically, the theorem can be stated as follows:
201+
202+
> A positive integer $n$ can be expressed as the sum of two squares if and only if all prime factors of $n$ of the form $4k + 3$ have even powers.
203+
204+
This means that if we decompose $n$ into the product of its prime factors, $n = p_1^{e_1} p_2^{e_2} \cdots p_k^{e_k}$, where $p_i$ are primes and $e_i$ are their corresponding exponents, then $n$ can be expressed as the sum of two squares if and only if all prime factors $p_i$ of the form $4k + 3$ have even exponents $e_i$.
205+
206+
More formally, if $p_i$ is a prime of the form $4k + 3$, then for each such $p_i$, $e_i$ must be even.
207+
208+
For example:
209+
210+
- The number $13$ is a prime and $13 \equiv 1 \pmod{4}$, so it can be expressed as the sum of two squares, i.e., $13 = 2^2 + 3^2$.
211+
- The number $21$ can be decomposed into $3 \times 7$, where both $3$ and $7$ are prime factors of the form $4k + 3$ and their exponents are $1$ (odd), so $21$ cannot be expressed as the sum of two squares.
212+
213+
In summary, this theorem is very important in number theory for determining whether a number can be expressed as the sum of two squares.
214+
215+
The time complexity is $O(\sqrt{c})$, where $c$ is the given non-negative integer. The space complexity is $O(1)$.
216+
217+
<!-- tabs:start -->
218+
219+
#### Python3
220+
221+
```python
222+
class Solution:
223+
def judgeSquareSum(self, c: int) -> bool:
224+
for i in range(2, int(sqrt(c)) + 1):
225+
if c % i == 0:
226+
exp = 0
227+
while c % i == 0:
228+
c //= i
229+
exp += 1
230+
if i % 4 == 3 and exp % 2 != 0:
231+
return False
232+
return c % 4 != 3
233+
```
234+
235+
#### Java
236+
237+
```java
238+
class Solution {
239+
public boolean judgeSquareSum(int c) {
240+
int n = (int) Math.sqrt(c);
241+
for (int i = 2; i <= n; ++i) {
242+
if (c % i == 0) {
243+
int exp = 0;
244+
while (c % i == 0) {
245+
c /= i;
246+
++exp;
247+
}
248+
if (i % 4 == 3 && exp % 2 != 0) {
249+
return false;
250+
}
251+
}
252+
}
253+
return c % 4 != 3;
254+
}
255+
}
256+
```
257+
258+
#### C++
259+
260+
```cpp
261+
class Solution {
262+
public:
263+
bool judgeSquareSum(int c) {
264+
int n = sqrt(c);
265+
for (int i = 2; i <= n; ++i) {
266+
if (c % i == 0) {
267+
int exp = 0;
268+
while (c % i == 0) {
269+
c /= i;
270+
++exp;
271+
}
272+
if (i % 4 == 3 && exp % 2 != 0) {
273+
return false;
274+
}
275+
}
276+
}
277+
return c % 4 != 3;
278+
}
279+
};
280+
```
281+
282+
#### Go
283+
284+
```go
285+
func judgeSquareSum(c int) bool {
286+
n := int(math.Sqrt(float64(c)))
287+
for i := 2; i <= n; i++ {
288+
if c%i == 0 {
289+
exp := 0
290+
for c%i == 0 {
291+
c /= i
292+
exp++
293+
}
294+
if i%4 == 3 && exp%2 != 0 {
295+
return false
296+
}
297+
}
298+
}
299+
return c%4 != 3
300+
}
301+
```
302+
303+
#### TypeScript
304+
305+
```ts
306+
function judgeSquareSum(c: number): boolean {
307+
const n = Math.floor(Math.sqrt(c));
308+
for (let i = 2; i <= n; ++i) {
309+
if (c % i === 0) {
310+
let exp = 0;
311+
while (c % i === 0) {
312+
c /= i;
313+
++exp;
314+
}
315+
if (i % 4 === 3 && exp % 2 !== 0) {
316+
return false;
317+
}
318+
}
319+
}
320+
return c % 4 !== 3;
321+
}
322+
```
323+
324+
<!-- tabs:end -->
325+
326+
<!-- solution:end -->
327+
194328
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool judgeSquareSum(int c) {
4+
int n = sqrt(c);
5+
for (int i = 2; i <= n; ++i) {
6+
if (c % i == 0) {
7+
int exp = 0;
8+
while (c % i == 0) {
9+
c /= i;
10+
++exp;
11+
}
12+
if (i % 4 == 3 && exp % 2 != 0) {
13+
return false;
14+
}
15+
}
16+
}
17+
return c % 4 != 3;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func judgeSquareSum(c int) bool {
2+
n := int(math.Sqrt(float64(c)))
3+
for i := 2; i <= n; i++ {
4+
if c%i == 0 {
5+
exp := 0
6+
for c%i == 0 {
7+
c /= i
8+
exp++
9+
}
10+
if i%4 == 3 && exp%2 != 0 {
11+
return false
12+
}
13+
}
14+
}
15+
return c%4 != 3
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean judgeSquareSum(int c) {
3+
int n = (int) Math.sqrt(c);
4+
for (int i = 2; i <= n; ++i) {
5+
if (c % i == 0) {
6+
int exp = 0;
7+
while (c % i == 0) {
8+
c /= i;
9+
++exp;
10+
}
11+
if (i % 4 == 3 && exp % 2 != 0) {
12+
return false;
13+
}
14+
}
15+
}
16+
return c % 4 != 3;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def judgeSquareSum(self, c: int) -> bool:
3+
for i in range(2, int(sqrt(c)) + 1):
4+
if c % i == 0:
5+
exp = 0
6+
while c % i == 0:
7+
c //= i
8+
exp += 1
9+
if i % 4 == 3 and exp % 2 != 0:
10+
return False
11+
return c % 4 != 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function judgeSquareSum(c: number): boolean {
2+
const n = Math.floor(Math.sqrt(c));
3+
for (let i = 2; i <= n; ++i) {
4+
if (c % i === 0) {
5+
let exp = 0;
6+
while (c % i === 0) {
7+
c /= i;
8+
++exp;
9+
}
10+
if (i % 4 === 3 && exp % 2 !== 0) {
11+
return false;
12+
}
13+
}
14+
}
15+
return c % 4 !== 3;
16+
}

0 commit comments

Comments
 (0)