The ancients had a recursive method for calculating a square root: Suppose you w
ID: 3673044 • Letter: T
Question
The ancients had a recursive method for calculating a square root: Suppose you want to find the square root of x, guess that the square root is x/2. Test your guess (what's the difference between x and guess*guess). If your guess isn't accurate enough, try again. Your new guess should be 0.5 * (last guess + X/last guess). Keep going until your guess is close enough. Your class should include a recursive method for calculating the square root using the Babylonian Method. Users of your class should be able to specify the number they'd like to take a square root of as well as how much error they will tolerate in the square root estimation. For testing purposes, you should print the output. babylonian method link: http://en.wikipedla.org/wiki/Methods of computing square roots#Babylonian method Make a main method to demonstrate that your class works.Explanation / Answer
Answer:
import java.util.*;
class Babylonian
{
public static void main(String args[])
{
Scanner input= new Scanner(System.in);
System.out.println("Enter an integer p");
int p = input.nextInt();
System.out.println("Square root of entered number is "+squareRootOfNumber(p));
}
public static float squareRootOfNumber(float p)
{
double e = 0.000001;
float a = p;
float b = 1;
while(a - b > e)
{
a = (a + b)/2;
System.out.println("Next Approximationalue value of a "+a);
b = p/a;
System.out.println("New guess value is "+b);
}
return a;
}
}
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.