Welcome to Dream.In.Code
Become a Java Expert!

Join 150,357 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,886 people online right now. Registration is fast and FREE... Join Now!




Babylonian Algorithm

 
Reply to this topicStart new topic

Babylonian Algorithm

CrazyJ
18 Feb, 2008 - 01:16 AM
Post #1

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
I coded this short program to compute the square root of an input number using iteration (Babylonian Algorithm). However, my goal now is to execute as follows:

1)make a guess at the answer
2)compute root = n/guess << n is the input number
3) set guess = (guess +r) / 2
4) Go back to step 2 until the last two guess values are the same.

I need to iterate until the guess is within 1% of the previous guess, and output the answer as a double to two decimal places. How can I cause the iteration to stop once I have two guess values that are the same? And also, how can I output the guess iterations as double to two decimal places using printf andhave them print on separate lines?
CODE

import java.util.Scanner;
public class Babylonian {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int n;
//        =============
        Scanner keyboard;
        keyboard = new Scanner(System.in);
        System.out.println("This program demonstrates the Babylonian algorithm.");
        System.out.println("");
//        =======================    
        System.out.println("Please enter a positive integer:");
        n = keyboard.nextInt();
        System.out.println("You entered "+n);
        System.out.println("");
//        =======================
        double guess = n/2;
        double r = 0.00;
        for(int ii = 0; ii < 10; ii++)
        {
            r = n / guess;
            guess = (guess + r) / 2;
            System.out.println(guess);
        }
//        ===================================================
        System.out.println("The square root of "+n+" is:");
        System.out.printf("  [ %4.2f ]", r);
//        =========================================      


    }// main

}//    class Babylonian


This post has been edited by CrazyJ: 18 Feb, 2008 - 02:07 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Babylonian Algorithm
18 Feb, 2008 - 04:03 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
You're using a for loop where you probably want a while loop. Make another variable called lastGuess and save the prior guess into it. Compare them in the while until they're close. You'll need to initialize lastGuess to some bogus number before you enter the loop.

Hope this helps.

User is online!Profile CardPM
+Quote Post

CrazyJ
RE: Babylonian Algorithm
20 Feb, 2008 - 02:15 PM
Post #3

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
QUOTE(baavgai @ 18 Feb, 2008 - 05:03 AM) *

You're using a for loop where you probably want a while loop. Make another variable called lastGuess and save the prior guess into it. Compare them in the while until they're close. You'll need to initialize lastGuess to some bogus number before you enter the loop.

Hope this helps.


Thanks baavgai...that did the trick! icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month