The following method was known to the ancient Greeks for computing square roots.
ID: 3761345 • Letter: T
Question
The following method was known to the ancient Greeks for computing square roots. Given
a value x > 0 and a guess g (for example guess =7) for the square root, a better guess is
(g + x/g) / 2.
Write a recursive helper method public static squareRootGuess(double x, double g). If
g2 is approximately equal to x (i.e |x – g2| < 0.0001), return g, otherwise, return
squareRootGuess with the better guess. Then write a method public static
squareRoot(double x) that uses the helper method.
Example:
x = 10; g =7
squareRootGuess(10, 7)
squareRootGuess(10, (7 + 10/7)/2) = squareRootGuess(10, 4.21428)
squareRootGuess(10, (4.21428 + 10/4.21428)/2) = squareRootGuess(10, 3.29358)
. . .
(continue until |10 – g2| < 0.0001)