Question

In: Computer Science

. (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 that inputs a double for n, iterates through the Babylonian algorithm until the guess is within 1% of the previous guess and outputs the answer as a double to two decimal places. Your answer should be accurate even for large values of n

Solutions

Expert Solution


import java.util.Scanner;

public class Babylonia {
    public static void main(String[] args) {
        int n;
        double guess;
        double r;
        Scanner input = new Scanner(System.in);
        System.out.println("This program estimates square roots.");
        System.out.print("Enter an integer to estimate the square root of: ");
        n = input.nextInt();
        System.out.println();
        guess = n / 2.0;
        r = 0;

        double last_guess = guess;
        do {
            r = n / guess;
            guess = (guess + r) / 2;
            last_guess = guess;
            System.out.println("Current guess: " + guess);
        }
        while (((guess - last_guess) / last_guess) > 0.01);
        System.out.printf("The estimated square root of %d is %4.2f", n, guess);

    }

}

Output Screenshot of above code


Related Solutions

Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian 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...
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...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by...
C++ Programming: Programming Design and Data Structures Chapter 13 Ex 2 Redo Programming Exercise 1 by overloading the operators as nonmembers of the class rectangleType. The header and implementation file from Exercise 1 have been provided. Write a test program that tests various operations on the class rectangleType. I need a main.cpp file Given: **************rectangleType.cpp******************** #include <iostream> #include <cassert> #include "rectangleType.h" using namespace std; void rectangleType::setDimension(double l, double w) { if (l >= 0) length = l; else length =...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
The goal of this exercise is to implement the shuffling algorithm from this chapter. 1. In...
The goal of this exercise is to implement the shuffling algorithm from this chapter. 1. In the repository for this book, you should find the file named Deck.java. Check that you can compile it in your environment. 2. Implement the randomInt method. You can use the nextInt method provided by java.util.Random, which we saw in Section 7.6. Hint: To avoid creating a Random object every time randomInt is invoked, consider defining a class variable. 3. Write a swapCards method that...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at...
Chapter 2, Problem 4E in An Introduction to Programming with C++ All of the employees at Merks Sales are paid based on an annual salary rather than an hourly wage. However, some employees are paid weekly while others are paid every other week (biweekly). Weekly employees receive 52 paychecks; biweekly employees receive 26 paychecks. The payroll manager wants a program that displays two amounts: an employee’s weekly gross pay and his or her biweekly gross pay. Complete an IPO chart...
Using a 5-bit version of booth algorithm, multiple 13 and -2. Verify your answer. please show...
Using a 5-bit version of booth algorithm, multiple 13 and -2. Verify your answer. please show all your steps
You'll implement a completely generic version of an algorithm to find the maximum of an array....
You'll implement a completely generic version of an algorithm to find the maximum of an array. Unlike in the past, when our algorithm only worked for int[] or double[], this version will work on any Java objects that are comparable, specifically any Java object that implements the Comparable interface. Create a public class named Max with a single class method named max. max should accept an array of Objects that implement Comparable and return the maximum. If the array is...
Write a version of the selection sort algorithm in a function called selectionSort that can be...
Write a version of the selection sort algorithm in a function called selectionSort that can be used to sort a string vector object. Also, write a program to test your algorithm. The program should prompt the user for a series of names. The string zzz should end the input stream. Output the sorted list to the console. *Need answer in C++*
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT