While Loops

4 / 5

A while loop can be used to approximate values iteratively. For example, Heron's method can be used to approximate the square root of a positive number $a$.

  1. Start with a guess $x_0$, for example, $x_0 = 0.1 \cdot a$.
  2. Then iteratively compute every next value by $x_{i+1} = \frac 1 2 \cdot (x_i + \frac S {x_i})$.
  3. Each time, check if $\left|x_i^2 - a\right|$ is close enough to zero; meaning smaller than some very small value of $\epsilon$ (epsilon).

Square root approximation

Input a nonnegative float a and a tolerance eps. Compute and print the square root of a with precision eps.

Congratulations!

You passed the !

Welcome!

Would you like a guided tour on how to use Vibewise?

Your email has been verified!