The Root Convergence
HardAcc. 81.7%
+40 XP 20
The Iterative Precision
Heron’s Method (Babylonian) finds square roots by repeatedly averaging: x = (x + target/x) / 2.
The Assignment
Your function receives parameters target and precision.
- Initialize
x = target / 2andsteps = 0. - Use a
whileloop that runs as long as the difference (Math.abs) betweenx * xandtargetis greater thanprecision. - In each step:
- Perform the update:
x = (x + target / x) / 2. - Increment
steps.
- Perform the update:
- Print the total number of
stepstaken to reach that precision.
01EXAMPLE 1
Input
target=16, precision=0.1Output
4Explanation: Four iterations to get within 0.1 of 16.
Constraints
- Stop strictly based on the precision threshold.
- Return the step count.
MathLoops
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
target16
precision0.1
Expected Output
4
Click RUN to test your solution