Sqrt(x)
# Solution
There are so many of them! Thanks to this question that I review Newton's method in Calculus.
Binary search may be easiest to write in an interview.
# Pocket Calculator Algorithm
Based on the formula (hard to come up with if haven't seen before):
Complexity
time:
space:
from math import e, log
def mySqrt(self, x):
if x < 2:
return x
left = int(e**(0.5 * log(x)))
right = left + 1
return left if right * right > x else right
2
3
4
5
6
7
8
# Binary Search
If , return .
For , is always smaller than and larger than 0: .
Complexity
time:
space:
def mySqrt(self, x):
if x < 2:
return x
left, right = 2, x // 2
while left <= right:
pivot = left + (right - left) // 2
num = pivot * pivot
if num > x:
right = pivot -1
elif num < x:
left = pivot + 1
else:
return pivot
return right
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Recursion + Bit Shifts
Because , we can do a recursion:
Could continue to optimize by bit shifts:
def mySqrt(self, x):
if x < 2:
return x
left = self.mySqrt(x >> 2) << 1
right = left + 1
return left if right * right > x else right
2
3
4
5
6
7
# Newton's Method
Newton's method (opens new window) gives the best runtime asymptotically:
The math is deduced below:
The following 3 solutions all use Newton's method in different forms.
# initialize sqrt
to x//2
Given the fact in binary search, return x
if x < 2
; otherwise, x//2
is an upper bound for and the in the while loop sqrt
monotonically decreases in every iteration.
def mySqrt(self, x: int) -> int:
if (x<2): return x
sqrt = x//2
while not (sqrt*sqrt<=x and (sqrt+1)**2>x):
sqrt = (sqrt + x//sqrt)//2
return sqrt
2
3
4
5
6
# initialize sqrt
to x
In the while loop, sqrt
monotonically decreases in every iteration.
def mySqrt(self, x: int) -> int:
sqrt = x
while sqrt**2>x:
sqrt = (sqrt + x//sqrt)//2
return sqrt
2
3
4
5
# initialize y
& z
The proof is sinuous and runtime is slower than the previous two solutions.
def mySqrt(self, x: int) -> int:
y = 1
z = x
while z > y:
z = (z+y)//2
y = x//z
return z
2
3
4
5
6
7
# Fast Approximations for Floating Sqrt
According to this video (opens new window), the floating sqrt can be approximated by the following methods.
# Continued Fraction
First approximate as , where . Then:
# Proof
# Long Division
Starting from 4:50 (opens new window), the method is presented.