1. ## The Babylonian algorithm

Estimate a square root of a number n:
1.make a guess (you can pick n/2);
2.compute r=n/guess;
3.set guess=(guess+r)/2;
4.Go back for step 2 and 3,until the guess is within 1% of the previous guess.the more,the closer.
OK,so we need a program to do it!
Code:
```#include<iostream>
using namespace std;
int main()
{
double n,r,gue,gue1;
cout<<"enter n:";
cin>>n;
gue=n/2;

do
{
r=n/gue;
gue1=gue;
gue=(gue+r)/2;
}while(gue>=0.01*gue1)

cout<<"It is "<<gue;
return 0;
}```

Look,I make gue1 to be the previous guess,so we can stop the program,but when I run it,it
is a infinite loop.Why?

2. The problem is with your loop condition (aside from the fact that you are missing a terminating semi-colon around there as well). You need to correctly express the negation of "the guess is within 1&#37; of the previous guess" in the loop condition.

Incidentally, just use guess as a variable name instead of gue. Two characters more does not hurt and can improve readability.

3. But I think I am correct，where is wrong?Could anyone tell me?

4. But I think I am correct,where is wrong?
Suppose gue1 is 100 and gue is 99. So, current guess is within 1&#37; of the previous guess.

gue >= 0.01 * gue1
99 >= 0.01 * 100
99 >= 1
true

So, when the current guess is within 1% of the previous guess, the loop continues to the next iteration. This is clearly wrong.

In fact, your current loop condition makes the loop loop until the current guess is less than 1% of the previous guess. However, the current guess cannot be less than 50% of the previous guess since r should be non-negative. As such, the loop is an infinite loop as the current guess cannot be less than 1% of the previous guess.

5. Yes,you are right!So there must be something wrong with this problem!Since it said within 1&#37;,but
actually gue=gue1/2+r/2means gue must be more than 50% of gue1,so it will never end!

6. Yes,you are right!So there must be something wrong with this problem!Since it said within 1&#37;,but
actually gue=gue1/2+r/2means gue must be more than 50% of gue1,so it will never end!
You are reading the problem incorrectly. 99 is within 1% of 100, but it is certainly more than 50% of 100.

7. ok,thx!I am so silly!