Square root

In calculating the square root of a positive real number $n$, the following algorithm is surprising efficient:

Start with an estimate of the square root of $n$, say $x_0 = n$.

Starting with $k$ taking on the value $0$, $1$, $2$, etc., let

$x_{k + 1} = \frac{1}{2}\left( x_k + \frac{n}{x_k} \right)$.

You will note that the expression $\frac{1}{2}\left( x_k + \frac{n}{x_k} \right)$ is the average value of the approximation $x_k$ and $\frac{n}{x_k}$, and if $x_k < \sqrt{n}$ then $\frac{n}{x_k} > \sqrt{n}$ and vice versa, so the average of these two should be closer to the actual value of $\sqrt{n}$ than either of the two values.

To estimate the square root of $2$, the we would start with $x_0 = 2$, and next

$x_1 = \frac{1}{2}\left( x_0 + \frac{n}{x_0} \right) = \frac{1}{2}\left( 2 + \frac{2}{2} \right) = \frac{3}{2} = 1.5$.

Next, we would calculate

$x_2 = \frac{1}{2}\left( x_1 + \frac{n}{x_1} \right) = \frac{1}{2}\left( 1.5 + \frac{2}{1.5} \right) = \frac{2.8\overline{3}}{2} = 1.41\overline{6}$.

Next, we could calculate

$x_3 = \frac{1}{2}\left( x_2 + \frac{n}{x_1} \right) = \frac{1}{2}\left( 1.41\overline{6} + \frac{2}{1.41\overline{6}} \right) = \frac{2.82\overline{8431372549019607}}{2} = 1.414\overline{2156862745098039}$.

Repeating this process, we could determine the first six approximations,

 $x_0 = 2$
 $x_1 = 1.5$
 $x_2 = 1.41666666666666666666666666666666666666666666666666667$
 $x_3 = 1.41421568627450980392156862745098039215686274509803922$
 $x_4 = 1.41421356237468991062629557889013491011655962211574404$
 $x_5 = 1.41421356237309504880168962350253024361498192577619743$
 $x_6 = 1.41421356237309504880168872420969807856967187537723400$
$\sqrt{2} = 1.41421356237309504880168872420969807856967187537694807$

As we are giving you this algorithm, your goal is to determine a rule to stop when we are guaranteed that there are at least $n$ correct digits in the approximation.

Knowing when to stop calculating is an important understanding required for engineering judgment: it is always possible to get a more accurate answer, but that also requires additional computation time. For example, you could simply keep calculating until no digits change in our approximation, but this may be unnecessarily wasteful if all that is required is four digits of precision.