Sorry 4 bothering you guys... its that im a beginner at this... they gave me a problem but I dont know how to interpret it, it says as follows:

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

(1) Make a guess, say g, at the answer (you can use n/2 for the initial guess).
(2) Compute r = n/g.
(3) Set g' = (g − r)/2. (g' is the guess for the next iteration)
(4) Go back to step 2 after setting g to the value of g'.
The more iterations of the loop you run, the closer you get to the square root of n.
Write a program that asks for a (double) number n and an (double) accuracy. Your
program is to loop until g2 is no more than accuracy away from n. Your program then
outputs g.
Hint Use more meaningful names than the one letter names above.

I just do not understand this, as I do not understand loops too well. Can any one of you "dummie" it down for me, you dont have to do me the excersise, just explain it a little easier, what do I hace to do, what does r, n & g exactly mean, how many times should I run the loop, what it is that I have to input.

If you can help me it would be appreciated :D

8 Years
Discussion Span
Last Post by ithelp

Wikipedia has a better explanation than the one you are going from above. I'd use it instead and toss out the one you posted. The notation is better. It also has an example using numbers. Each iteration is based on the previous iteration and will get you progressively closer to the solution. You stop when two successive iterations are within your margin of error that the user types in. You need some method to specify an initial guess. Your problem description suggests n / 2, so you can use that. Get a calculator and go through a few examples on paper till you really understand the formula and what's going on. THEN start coding it.



(3) Set g' = (g − r)/2. (g' is the guess for the next iteration)

For it to work, the above should be: Set g' = (g + r)/2.


Use a while loop and at the end of while loop check whether abs(g-g') < required precision if yes break out of the while loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.