Previous

While Loops

4 / 4
Next

A while loop can be used to approximate the square root of a value $S$ iteratively.

  1. Start with a guess $x$, for example, $x = 0.1 \cdot S$.
  2. If $\left|x^2 - a\right| < \epsilon$ for some very small error tolerance $\epsilon$ (epsilon) then $x$ is a good enough solution.
  3. Otherwise, a better estimate is $\frac 1 2 \cdot (x + \frac S {x})$.

Repeat steps 2 and 3 until the condition $\left|x^2 - a\right| < \epsilon$ holds.

Send

Capstone

Hint
Advanced

Input a float S and an error tolerance epsilon. Approximate the square root of S with error tolerance epsilon.

Hint: the absolute value $\left| x^2 -a \right|$ can be computed using the Python function abs(x**2 - a).

Help
Solve
Reset
Run
Submit
 

Test results

Test result #
Expected output
Your output

Your email has been verified!