Skip to content

Commit 523dec5

Browse files
committed
Check if it is perfect square number
1 parent 1dc6e36 commit 523dec5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

algorithms/math/perfect_square.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def is_perfect_square(num):
2+
"""
3+
Given a positive integer num, write a function which returns True if num is a perfect square else False.
4+
5+
Note: Do not use any built-in library function such as `sqrt`.
6+
7+
:type num: int
8+
:rtype: bool
9+
"""
10+
i = 0
11+
while i * i < num:
12+
i += 1
13+
if i * i == num:
14+
return True
15+
else:
16+
return False
17+
18+
19+
# Test
20+
print(is_perfect_square(16))
21+
print(is_perfect_square(14))

0 commit comments

Comments
 (0)