x
is less than 2, return x
as the square root is either x
itself or 0.left
to 2 and the right boundary right
to x / 2
.left
is less than or equal to right
:
a. Calculate the guess num
as the average of left
and right
(integer division).
b. If num * num
is greater than x
, set right
to num - 1
.
c. If num * num
is less than x
, set left
to num + 1
.
d. If num * num
equals x
, return num
as the square root.right
as the integer square root of x
.