Floor of a Square Root

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write intSqrt(n): n is a whole number from 0 to 2,147,483,647, which is 2 to the power 31, minus 1. Return the square root of n rounded down: the largest whole number whose square is at most n. Work it out yourself, without Math.sqrt, Math.pow or the ** operator; the checks can't tell, but that's the exercise. For example, intSqrt(8) is 2 and intSqrt(16) is 4. The checks call it 100,000 times on numbers over 100 million, so counting up from 1 is too slow.

Your code must pass

  • Returns the exact root of a perfect square
  • Rounds down between squares
  • Handles 0, 1 and the largest input
  • Is fast enough for 100,000 large numbers