4.2k views

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}}$
| 4.2k views
0
In question it is mentioned e>0

This condition is very ambiguous as assuming e much greater than x will result in x itself ( loop condition fails).

I understand , in exam most appropiate option is to be chosen

But what if, another option given as 'x'

?

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$
by Junior (775 points)
edited
+1
The input value of X in expression 1 is different from th einput value of X in expression 2.

How can we substitute expression 2 in 1? we need to do vice varsa right? though both gives same answer
+1

Yes, the ans draw is seeming correct.

But i think @exam we cant rely and put ans like this.

Reason1->>In Ans they have not considered condition  While (x-y > ϵ)

Reason2->>How can u put step2 in step1 without executing step1 cz Program runs sequentially.

0
can someone tell the time complexity of this code?? is itO( log log n)???
0

>>>Yes, time complexity is O(loglogn) as the subproblem size is getting shrinked by √n

I think this statement is not correct. This is a numerical method of computing square root.

0
Output will be different for different values of $\epsilon$.
0
I think none of the answers is correct here ... assume m = 4 and epsilon = 8

(4-1=3) is not greater than 8

so print x which is 4

None of them matches right . ?
0
How can u put the y value in x=(x+y)/2
+1
Since when do we consider the "=" operator as an equal to sign? Its a value assignment operator and by that logic statements like a= a+b would be completely absurd. We cannot make a quadratic equation out this even if it might be close to the answer.
0

Refer to @sakht launda  answer and link (in the bottom most part) , that seems accurate enough to answer all queries and aspect related to code and complexity as well.

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.

by (193 points)

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 .

by Boss (23.9k points)
0

In 3rd iterarion How are you getting 3.14 ?
For me the loop stops at rd iteration and output x as 2.75  .  But your trace has got 4 iterations .Moreover Your option D is 3 . It should be 2 .

Tough the answer does not change much

This is an implementation of Babylonian method for square root - http://www.geeksforgeeks.org/square-root-of-a-perfect-square/

its time complexity is - log(log(n/e)) -https://stackoverflow.com/questions/12309432/time-complexity-for-babylonian-method

by (233 points)