Whole Square Root

EasyBinary Search≈ 15 min

Given a whole number x, return its whole square root: the largest whole number r with r × r ≤ x. In other words, the square root of x with everything after the decimal point dropped.

Do not use a built-in square root or power function: the point is to find r yourself.

Examples

text
x = 16   →  4
x = 20   →  4
x = 0    →  0
x = 1    →  1

4 × 4 = 16 is at most 20, and 5 × 5 = 25 is not, so the whole square root of 20 is 4.

Constraints

  • x is between 0 and 10,000,000,000.
  • Aim for O(log x): trying 0, 1, 2, … in turn takes about √x steps.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 4 visible cases here, in your browser. Submit runs all 9 on the judge, 5 of them hidden, and measures how your solution scales.