5. The following method was known to the ancient Greeks for computing square roo
ID: 3712171 • Letter: 5
Question
5. The following method was known to the ancient Greeks for computing square roots. Given a value (a+e) x > 0 and a guess g for the square root, a better guess is. Write a recursive helper method squareRootGuess(double x, double g). If g2 is approximately equals to x (within .0001 of x) return g, otherwise, keep going until you get a better guess. Then write a method squareRoot(double x) that uses the helper method. You cannot use Math.sqrt() in solving this problem. Hint: if this method really works, does it matter what the initial guess is?Explanation / Answer
public class Test { public static double tol = 0.0001;//required accuracy in the squareroot public static void main(String args[]) { //value of x whose square root is to be found double x = 225; //printing the square root of x by calling squareRoot function System.out.println(squareRoot(x)); //output:= 15.000001132796916 (actual answer = 15) } public static double squareRootGuess(double x,double g) { //checking if we are getting the required accuracy and returning ans if(Math.abs(x - g*g)Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.