Skip to content

Commit 559b6ba

Browse files
authored
Create Day-25_Sum_of_Squares.py
1 parent 71740f0 commit 559b6ba

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Solution - 1: PreComputation
2+
class Solution:
3+
def judgeSquareSum(self, c: int) -> bool:
4+
threshold = (1<<31) - 1
5+
pre = set()
6+
i = 0
7+
while True:
8+
temp = i*i
9+
if temp > threshold:
10+
break
11+
else:
12+
pre.add(temp)
13+
i += 1
14+
15+
for i in pre:
16+
val = c - i
17+
if val in pre:
18+
return True
19+
20+
return False
21+
22+
23+
24+
# Solution - 2
25+
import math
26+
class Solution:
27+
28+
def judgeSquareSum(self, c: int) -> bool:
29+
for a in range(int(math.sqrt(c))+1):
30+
b = math.sqrt(c - a*a)
31+
if b == int(b):
32+
return True
33+
34+
return False

0 commit comments

Comments
 (0)