Square Root - iteratively
Previous    Next    Home    Source    Package

In the applet on the left, enter a number in the input box and hit return. The square root of that number shows up in the output box and the progression of m appears in the JTextArea.

Square Root from 2000 B.C.
  1. Problem: Fix the problem of iteration uncertainty in finding square roots using the method of the previous slide.
  2. Introduce a precision variable of type double (in this case it is initialized to 0.0000001) and change the for loop to a do loop so as to test whether m differs from that of the previous iteration by the precision amount. If the difference is really small then break out of the loop and print the result.
  3. A do is used instead of a while because the condition needs to be tested after m is computed at least once.
  4. A JTextArea has been added to see the progression toward the square root.