Question

In: Computer Science

Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:


Chapter 3 Exercise 1
Babylonian Algorithm.
The Babylonian algorithm to compute the square root of a positive number n is as follows:

      1. Make a guess at the answer (you can pick n/2 as your initial guess).
      2. Computer = n / guess.
      3. Set guess = (guess +r) / 2.
      4. Go back to step 2 until the last two guess values are within 1% of each other.

Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess is within 1% of the previous guess, and outputs the answer as a doubleto two decimal places. Your answer should be accurate even for large values of n.

INPUT and PROMPTS. The program prints "This program estimates square roots." and then prompts for a positive integer as follows: ("Enter an integer to estimate the square root of: " and then reads in the integer.

OUTPUT. Each time a new guess is computed, the program prints ithe line "Current guess: g", where g is the current guess, printed out with no special formatting. The final output is "The estimated square root of x is y", where x is the integer that was read in and y is the value computed by the procedure described above, printed out to two decimal places and a toal of six characters.

CLASS NAMES. Your program class should be called Babylonia


Solutions

Expert Solution

Answer:

import java.util.Scanner;

public class BabylonianAlgorithm
{  
   /*The Babylonian algorithm to compute the square root of a positive number n is as
   follows:
   1. Make a guess at the answer (you can pick n /2 as your initial guess).
   2. Compute r = n / guess
   3. Set guess = (guess +r)/ 2
   4. Go back to step 2 for as many iterations as necessary. The more you repeat steps
       2 and 3, the closer guess will become to the square root of n .
   Write a program that inputs a double for n , iterates through the Babylonian algorithm
   ten times, and outputs the answer as a double to two decimal places. Your
   answer will be most accurate for small values of n.
   */
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       Double n, r, guess;
      
       System.out.println("Please enter a positive number to compute it's square root.");
       n = input.nextDouble();
      
       //initial guess n/2
       guess = n / 2;
      
       for(int i = 0; i < 10; i++)
       {
           r = n / guess;
           guess = (guess + r) / 2;
       }
      
       System.out.print("The guess for the the square root of " + n);
       System.out.printf(" is %.2f.%n", n, guess);
      
       input.close();
   }
}


Related Solutions

1.a.)Use the assumed Babylonian square root algorithm (also known as Archimedes’ method) √ a 2 ±...
1.a.)Use the assumed Babylonian square root algorithm (also known as Archimedes’ method) √ a 2 ± b ≈ a ± b/2a to show that √ 3 ≈ 1; 45 by beginning with the value a = 2. Find a three-sexagesimal-place approximation to the reciprocal of 1; 45 and use it to calculate a three-sexagesimal-place approximation to √ 3. 1.b)An iterative procedure for closer approximations to the square root of a number that is not a square was obtained by Heron...
write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
A positive number is added to the reciprocal of its square root. Use Calculus methods to...
A positive number is added to the reciprocal of its square root. Use Calculus methods to answer the following questions:what if anything, is the smallest such sum? What, if any, positive x-values will yield the smallest sum? Give exact answers as well as answers accurate to Five decimal place. Be sure to give a mathematical justification that you answer represents a minimum and not a maximum.
prove that positive operators have unique positive square root
prove that positive operators have unique positive square root
2. Give a recursive algorithm to compute the product of two positive integers, m and n,...
2. Give a recursive algorithm to compute the product of two positive integers, m and n, using only addition and subtraction. Java Language...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy...
Design an efficient algorithm to compute the number of binary strings with length n that satisfy 1 the regular expression ((0 + 11 + 101)∗ (1101))∗ .
Deduce from the Completeness Axiom that there exists a square root of a real number a...
Deduce from the Completeness Axiom that there exists a square root of a real number a if and only if a ≥ 0
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to...
. (This is a version of Programming Project 2.1 from Chapter 2.) The Babylonian algorithm to compute the square root of a positive number n is as follows: 1. Make a guess at the answer (you can pick n/2 as your initial guess). 2. Compute r = n / guess. 3. Set guess = (guess +r) / 2. 4. Go back to step 2 until the last two guess values are within 1% of each other. Write a JAVA program...
Write three functions that compute the square root of an argument x using three different methods....
Write three functions that compute the square root of an argument x using three different methods. The methods are increasingly sophisticated, and increasingly efficient. The square root of a real number x is the value s such that s*s == x. For us, the values will be double precision variables and so may not be perfectly accurate. Also, for us, assume that x is in the range 0.0 to 100.0. You program should have a main() that asks the user...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT