Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The programm should take the (final) number x and calculate the square root of it. It shall stop after n iterations. Furthermore it shall be solved with recursion.


My code so far:

Java
public class Babylon{

    double sqrt( final double x, final int n ) {

        double s;

        if ( n == 0 ) {
            return x;
        }



        else {
            s=sqrt(0.5*(x+x/x),n-1); // I guess that the error must be here
            return 0.5*(s+x/s);
        }
    }       

public static void main (String[] args) {

    Babylon t = new Babylon();

    t.sqrt(10,8); // That is for testing reasons.


}
}   


What I have tried:

I tried it with "s=sqrt(0.5*(s+x/x),n-1)"; and all variations of the formula. Also tried to put the "sqrt" right after the return statement.
Posted
Updated 4-Nov-17 2:31am

You need to do something like this:
double sqrt(double S, double x, double n)
{

    if (n == 0)
        return x;
    else
        return sqrt(S,0.5*(x+S/x), n - 1);

}
S will never change since its the number you want the square root of, while x will change. And you have to guess an initial x value to start with which cannot be zero, let us say 0.1, so the call can be like this:
x_n = sqrt(S, 0.1, 10);
If you think about it you are trying to find when x^2 = S, and we know that when x = sqrt(S) that S/x = x, whitch in turn means that 0.5*(x+S/x) = x.
 
Share this answer
 
v3
I don't know what you try to do, but:
x/x is 1, and (x+x/x) is x+1
Guessing you want to use the Babylonian method, you need to keep track of the square root you search and its current estimate.
Methods of computing square roots - Wikipedia[^]

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v3
Comments
Member 13502303 4-Nov-17 5:34am    
Thank you for your answer. Yes, you're right. The point is it to use the Babylonian method for calculating square roots.

Furthermore I am not sure if a debugger can help me solve the problem. I already know that some weird "s" is going to be returned by the function. I just don't know how I should write the code different so that it works like:

square root x =

Estimate 1: 0.5 * (s(0) + x / (s0)) = 0.5*(x+1)

Estimate 2: 0.5 * (0.5*(x+1) + x / (0.5*(x+1)))

.
.
.
Patrice T 4-Nov-17 6:15am    
Follow the first link, you have a, example with values in formulas.
As long as you don't get those values, you are wrong.

The debugger allow you to see what is doing the code.
Member 13502303 4-Nov-17 7:11am    
I used the link and tried it out. Didn't help at all. Still, thanks for trying to help me.
Patrice T 4-Nov-17 7:27am    
Take a sheet od paper, a pencil and solve the problem by hand.
reread link until you understand what you do.
Member 13502303 4-Nov-17 7:45am    
That's what I am doing for nearly one week now. I (guess that I) understand the algorithm but am unable to implement it.

The point is to get to s_n. Than s_n insert in s_(n-1). Than s_(n-1) in s_(n-2) and so on. => Some kind of formula e.g. f(s)

And finally x = 0.5*(f(s) + x / f(s))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900