14,753 views
33 votes
33 votes

What does the following algorithm approximate? (Assume $m > 1, \epsilon >0$).

x = m;
y = 1;
While (x-y > ϵ)
{
    x = (x+y)/2;
    y = m/x;
}
print(x);
  1. $\log \, m$
  2. $m^2$
  3. $m^{\frac{1}{2}}$
  4. $m^{\frac{1}{3}}$

6 Answers

Best answer
76 votes
76 votes
By putting $y = m/x$ into $x = ( x + y )/2$
$\quad x= ( x + m/x ) /2$

$\implies 2x^2= x^2 + m$
$\implies x = m^{1/2}$

We can also check by putting $2$ or $3$ different values also.

Correct Answer: $C$
edited by
28 votes
28 votes

First of all this question is about approximation of value. So we can do 1 step more or less. That will not affect the answer.

Answer given by @gate_asp is somehow appropriate, but it doesn't answer some questions which are mentioned in comments. 

So here is my ans. 

loop will terminate in (Log Log n) iterations. at that time if x is a perfect square number then it x = y = $m^{1/2}$

otherwise it will terminate with condition y > x.

21 votes
21 votes

take m=8 and trace the Algorithm and verify the options A.3  B.64  C.2$\sqrt{2}$=2.82  D.3

Imp Note:-> we cant take variable type as int (cz it is an algorithm as specified in question) if U do so we end up with incorrect result

Now Trace the algorithm

Initially m=8 , $\epsilon$=1 , x=8 , y=1

Iteration # x y while(x - y >1)
1 8 1     True
2 4.5 1.78     True
3 3.14 2.55     True
4 2.85 2.8      False

So it match with Option C .

Answer:

Related questions

8.3k
views
3 answers
15 votes
Kathleen asked Sep 18, 2014
8,255 views
Consider the following C programmain() { int x, y, m, n; scanf("%d %d", &x, &y); /* Assume x>0 and y>0*/ m = x; n = y; while(m != n ... repeated subtractionthe greatest common divisor of $x$ and $y$the least common multiple of $x$ and $y$
18.2k
views
7 answers
47 votes
Kathleen asked Sep 18, 2014
18,179 views
The recurrence equation$ T(1) = 1$T(n) = 2T(n-1) + n, n \geq 2$evaluates to$2^{n+1} - n - 2$2^n - n$2^{n+1} - 2n - 2$2^n + n $
19.8k
views
6 answers
31 votes
Kathleen asked Sep 18, 2014
19,793 views
The time complexity of the following C function is (assume $n > 0$)int recursive (int n) { if(n == 1) return (1); else return (recursive (n-1) + recursive (n-1)); }$O(n)$O(n \log n)$O(n^2)$O(2^n)$
20.8k
views
13 answers
66 votes
Kathleen asked Sep 18, 2014
20,821 views
Let $A[1,\ldots,n]$ be an array storing a bit ($1$ or $0$) at each location, and $f(m)$ is a function whose time complexity is $\Theta(m)$. Consider the following program ... (n^2)$\Omega (n\log n) \text{ and } O(n^2)$\Theta(n)$o(n)$