Lesson goal: Finding Square roots

Previous: Adding numbers to find Pi | Home | Next: Iterating a function

Suppose you were given a number and asked to find its square root? How would you do it? The great Isaac Newton thought about such puzzles, and came up with the following method.

Say you want to take the square root of a number $x$. Newton's method always started with a guess. But what to guess? Well, we know the square root of a number is less than the number itself, like $\sqrt{25}=5$, $\sqrt{625}=25$, and $\sqrt{10}\approx 3.2$ so our guess should always be less than the number itself. How about the number divided by two? So our initial guess at the $\sqrt{x}$ will be $x/2$ or $g=x/2$.

The idea of Newton's method is to keep correcting our guess until a better and better result for the square root of our original number appears. So, we'll correct our guess to be $z$ where $z=g-\frac{g^2-x}{2\times g}$ (this is a formula Newton came up with). When done, $z$ will be a better estimate of the square root than our intial guess.

What do we do next? That's right! Call $z$ our new [and better] "guess" and run the procedure over again. If we keep doing this again and again, a pretty decent result for the square root of our number will emerge.

Let's write a function called mysqrt that we can call to find square roots using Newton's method.

Now you try. Using fixing the sqrt function according to the discussion above to implement Newton's "guess and check" method for finding square roots.

Type your code here:

See your results here: