Skip to content

Commit f6573e5

Browse files
authored
Create check_sum_of_square_numbers.py
1 parent b90bae6 commit f6573e5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

check_sum_of_square_numbers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# coding: utf-8
2+
3+
class Solution:
4+
"""
5+
@param: : the given number
6+
@return: whether whether there're two integers
7+
"""
8+
def checkSumOfSquareNumbers(self, num):
9+
# write your code here
10+
if num < 0:
11+
return False
12+
import math
13+
upper = int(math.sqrt(num))
14+
for i in range(upper + 1):
15+
start, end = i, upper # 二分法的干活
16+
while start <= end:
17+
j = int((start + end) / 2)
18+
s = i * i + j * j
19+
if s == num:
20+
return True
21+
elif s > num:
22+
end = j - 1
23+
else:
24+
start = j + 1
25+
return False
26+
27+
# easy: http://lintcode.com/zh-cn/problem/check-sum-of-square-numbers/

0 commit comments

Comments
 (0)