The Square Root Estimator

MediumAcc. 86.1%
+30 XP 12

Finding the Core

You need to find the largest integer $x$ such that $x^2 le target$.

The Assignment

Your function receives a parameter named target.

  1. If target is 0 or 1, print the target and exit.
  2. Initialize low to 1 and high to target.
  3. Use a while loop (low <= high) to search for the root:
    • Find the middle point (mid = Math.floor((low + high) / 2)).
    • If mid * mid === target, you found it! Print mid and exit.
    • If mid * mid < target, move low = mid + 1 and store mid as the "best answer so far".
    • If mid * mid > target, move high = mid - 1.
  4. After the loop, print the best mid that didn't exceed the target.

01EXAMPLE 1

Inputtarget = 8
Output2

Explanation: 2^2=4, 3^2=9. Highest integer not exceeding 8 is 2.

Constraints

  • Do not use Math.sqrt().
  • Must use binary search or iterative approximation.
MathLoops
JavaScript
Loading...
3 Hidden

Input Arguments

target16

Expected Output

4

Click RUN to test your solution