edited by
18,106 views
59 votes
59 votes

Consider the following function.

double f(double x){
    if( abs(x*x - 3) < 0.01) 
        return x;
    else 
        return f(x/2 + 1.5/x);
} 

Give a value $q$ (to $2$ decimals) such that $f(q)$ will return $q$:_____. 

edited by

4 Answers

Best answer
98 votes
98 votes

(We can directly go to the "if" part to get one answer, but we need to solve "else" part too to get all possible answers which though is not asked in question)

Solving the else part:

$\frac{x}{2} + \frac{3}{2x} = \frac{x^2+3}{2x}$

So, the new value of $x$ will be  $\frac{x^2+3}{2x}$ and we need it equal to $x$.

$\frac{x^2+3}{2x} = x \\ \implies x^2 + 3 = 2x^2 \\ \implies x^2 = 3  \\ \implies x = 1.732 $


Now solving the if part.   

  abs(x*x - 3) < 0.01

So, $x^2 - 3 < 0.01  \text { and } -\left(x^2 - 3\right) < 0.01\\ \implies x^2 < 3.01  \text{ and } x^2 > 2.99\\ \implies x < 1.735 \text { and }x > 1.729$

Corrected to $2$ decimal places answer should be $1.73$ or $1.74$. 

edited by
9 votes
9 votes

I fell  we dont need q i got q as 1.735 it is said for what of  q

f(q) returns   q

so if  x2-3 <0.01 only then f(x) would return x  

so on solving x= (3.01)1/2  

someone please rectify me if m wrong and make me understand

0 votes
0 votes
Step 1: abs() return positive value , eg abs(-2)=2;

If abs(x*x-3) return a value which is less than 0.01, then it condition satisfy and return x;

So, x*x-3<0.01 => x<1.732

Step 2: Recheck f(1.73) will return 1.73 or not?
Answer:

Related questions

47 votes
47 votes
3 answers
3
go_editor asked Sep 28, 2014
13,512 views
The number of distinct minimum spanning trees for the weighted graph below is _____
42 votes
42 votes
6 answers
4
go_editor asked Sep 28, 2014
17,313 views
A cycle on $n$ vertices is isomorphic to its complement. The value of $n$ is _____.