Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29bd2cc

Browse files
committedJun 17, 2024
@Solution: Sum of Square Numbers
1 parent 73c2337 commit 29bd2cc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
 
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Sum of Square Numbers
2+
3+
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
4+
5+
Example 1:
6+
7+
```
8+
Input: c = 5
9+
Output: true
10+
Explanation: 1 * 1 + 2 * 2 = 5
11+
```
12+
13+
Example 2:
14+
15+
```
16+
Input: c = 3
17+
Output: false
18+
```
19+
20+
**Constraints:**
21+
- 0 <= c <= 2^31 - 1
22+
23+
[Link](https://leetcode.com/problems/sum-of-square-numbers/description/?envType=daily-question&envId=2024-06-17)
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
class Solution:
3+
def judgeSquareSum(self, c: int) -> bool:
4+
i = 2
5+
while i * i <= c:
6+
count = 0
7+
if c % i == 0:
8+
while c % i == 0:
9+
count += 1
10+
c /= i
11+
if i % 4 == 3 and count % 2 != 0:
12+
return False
13+
i += 1
14+
return c % 4 != 3
15+
16+
17+
s = Solution()
18+
print(s.judgeSquareSum(5))
19+
print(s.judgeSquareSum(3))
20+
print(s.judgeSquareSum(6))
21+
print(s.judgeSquareSum(8))

0 commit comments

Comments
 (0)
Please sign in to comment.