Sqrt(x)
Description:
Implement int sqrt(int x).
Compute and return the square root of x.
Example
sqrt(3) = 1
sqrt(4) = 2
sqrt(5) = 2
sqrt(10) = 3
Challenge
O(log(x))
Code:
1.
class Solution:
"""
@param x: An integer
@return: The sqrt of x
"""
def sqrt(self, x):
# write your code here
return math.floor(math.sqrt(x))
2.
class Solution:
"""
@param x: An integer
@return: The sqrt of x
"""
def sqrt(self, x):
# write your code here
if x == 0:
return 0
if x == 1:
return 1
for i in range (x):
if (i*i <= x) and ((i+1)*(i+1)>x):
return i