-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalid.py
More file actions
35 lines (30 loc) · 910 Bytes
/
valid.py
File metadata and controls
35 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
"""
# Brute Force with some decision making
# MemoryError
score = num//2
for i in range(score):
if i**2 == num:
return True
return False
"""
"""
# 100 pass - Perfect Square: Instead of checking square of all the number in the range, calculate the mid and find the last number which is less than the mid of the num. Square of that number must be the perfect square
i = 1
while i < num/i:
i += 1
if i*i == num:
return True
else:
return False
"""
# Newton Method
x = num
while x*x > num:
x = (x + num/x)/2
return x*x == num