You are to write a program that implements the Babylonian algorithm to compute t
ID: 3629911 • Letter: Y
Question
You are to write a program that implements the Babylonian algorithm to compute the square root of a number. The steps of the algorithm are1. Get an input number
2. Compute an initial guess…such as input number /2.
3. Compute an intermediate number = input number / initial guess
4. Compute a second guess = (initial guess + intermediate)/2.
5. Set initial guess = second guess
6. Go back to step 3
7. Continue for a set number of integrations or until desired accuracy is achieved
Your program is to use a for that iterates 10 times. You are to output the second guess of each iteration
Here are some sample outputs:
This program will find the square root of a number that is entered using a iterative method from the Babylonians.
Find the square root of: 1234
309.5
156.7435379644588
82.30813540608735
48.650289029840586
37.0074946572093
35.17604589177702
35.128368495194096
35.128336140515486
35.12833614050059
This program will find the square root of a number that is entered using a iterative method from the Babylonians.
Find the square root of: 144
37.0
20.445945945945947
13.744453475286258
12.110703489698958
12.000505968238837
12.000000010666378
12.0
12.0
12.0
This program will find the square root of a number that is entered using a iterative method from the Babylonians.
Find the square root of: 999
250.75
127.36702392821536
67.60524919714156
41.19110413132753
32.72195679634224
31.62595790745999
31.606966963882158
31.606961258558734
31.606961258558215
It is to be written in java
Explanation / Answer
import java.util.Scanner;
public class Babylonian {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("This program will find the square root of a number that is entered using a iterative method from the Babylonians.");
System.out.print("Find the square root of: ");
n = sc.nextInt();
double guess = n/2;
System.out.println(guess);
double r = 0.00;
for(int ii = 1; ii < 10; ii++)
{
r = n / guess;
guess = (guess + r) / 2;
System.out.println(guess);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.