The Gateway to Computer Science Excellence
+22 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;
  1. $\log \, m$
  2. $m^2$
  3. $m^{\frac{1}{2}}$
  4. $m^{\frac{1}{3}}$
in Algorithms by Veteran (52.2k points) | 4.2k views
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'


4 Answers

+44 votes
Best answer
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 by
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

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.

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

@Ayush Upadhyaya 

>>>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.  

Output will be different for different values of $\epsilon$.
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 . ?
How can u put the y value in x=(x+y)/2
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.

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.

+16 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.

by (193 points)
+10 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 .

by Boss (23.9k points)

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

+8 votes

This is an implementation of Babylonian method for square root -

its time complexity is - log(log(n/e)) -

by (233 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,341 answers
105,212 users