Menu
CoddyTech

Square Root (Integer)

Your function gets a non-negative integer x and returns its integer square root: the largest integer r with r × r ≤ x. That is the square root rounded down, so a number that is not a perfect square gets the root of the perfect square below it. Compute it yourself, without a built-in square root or power function.

Function

mySqrt(x: integer) → integer
xinteger
the non-negative integer to take the square root of
Returnsinteger
the square root of x rounded down to an integer

Constraints

  • 0 ≤ x ≤ 231 - 1
  • Do not call a built-in square root, power or exponent function.

Examples

Input
x = 17
Output
4
Explanation
4 × 4 = 16 is at most 17, but 5 × 5 = 25 is more, so the root of 17 rounds down to 4.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

How would you find the integer cube root instead, the largest r with r × r × r ≤ x, if x could also be negative?

Reset code
def mySqrt(x):
    # Write code here
Test cases

Case 1

Case 2

Input

x = 17

Expected

4