What does the following algorithm approximate? (Assume ).
x = m;
y = 1;
While (x-y > ϵ)
{
x = (x+y)/2;
y = m/x;
}
print(x);View worked solution
Answer: C
To understand what the algorithm approximates, let us trace the variables and through the loop iterations.
1. Initial State:
Initially, and . Note that their product is .
2. Loop Invariant:
Inside the while loop, the variables are updated as follows:
- (Arithmetic Mean of and )
3. Termination Condition:
The loop terminates when the difference between and becomes extremely small, i.e., .
At this point, we have:
Substituting this into our invariant gives:
This is the classic Babylonian method (a special case of the Newton-Raphson method) for computing the square root of a number. Therefore, the algorithm approximates .
Free practice · Your answers on this page are not saved.
