in Programming in C edited by
18,100 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$:_____. 

in Programming in C edited by
18.1k views

9 Comments

WHY WE ARE NOT TAKING NEGATIVE VALUE i.e -1.73 becz x has two roots why we decided to take only positive value.
3
3
edited by

@eyeamgj

( sorry for the mistake, yes it should includes negative values also )

x$^2$ < 3.01  ====> x < 1.735 and x >  -1.735

and 

x$^2$ > 2.99⟹ x > 1.729 or x < -1.729

4
4

  CAN U SHOW INTERVALS NOT GETTING .

0
0

 OK GOT THE INTERVALS BUT HOW WE ARE ACTUALLY DECIDING VALUE FOR X.?

0
0
Beautiful question…. proves that paper setters are not putting literally any random statements in if and else but putting relevant expressions in if else blocks :)
0
0
edited by

value can’t be negative because $f(x)$ should return $x$ and not $-x$

0
0

@Shaik Masthan sir

$x^2$ < 3.01  ====> x < 1.735 or x >  -1.735

isn’t this must be and?  because if it is or then it covers whole real line.

although in 2nd or is ok.

1
1

@neel19 nhi bhai,baat sirf f(q) q return krega aisi hai

q negative ho sakta hai

1
1

4 Answers

97 votes
97 votes
Best answer

(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
by

4 Comments

@Argharupa Adhikary Yes you are correct. Both $-1.73$ and $1.73$ satisfy the program. Hence ans should be $-1.73$ or $1.73$ (in to $2$ decimals)

@gatecse Sir pls look at this.

0
0

Code :  The code is in python, since C was giving TLE (prob due to formatting issues).

I have attached the outputs here in form of tuple $(x, f(x))$. The bold values are the ones where f(x) was actually giving x as output.

(1.726, 1.7320614136732329)
(1.728, 1.7320555555555557)
(1.73, 1.73)
(1.732, 1.732)
(1.734, 1.734)
(1.736, 1.7320552995391705)
(1.738, 1.7320609896432682)
(1.74, 1.7320689655172414)
(-1.74, -1.7320689655172414)
(-1.738, -1.7320609896432682)
(-1.736, -1.7320552995391705)
(-1.734, -1.734)
(-1.732, -1.732)
(-1.73, -1.73)
(-1.728, -1.7320555555555557)

 

We can notice both $1.73$ and $-1.73$ gives correct answer. While $1.74$ or $-1.74$ does not give desired output.

@gatecse Sir, @ankitgupta.1729 Sir.

2
2

THANKS FOR EXPLAINING @Abhrajyoti00

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

by

2 Comments

Your answer was right. But I didn't understand what have you done in this question? You have taken square root of 3.01. right. But how have you decided to add 3 and 0.01 and find out its answer?

I didn't get such question? Can you tell me what we have to do in this question and how did you find out its answer?
0
0

actualy if you notice  you will see it is a maths saying for what value of the expression x2-3 <0.01 

i am saying this the maths because see in the code it is said when ever this condition is happening only then the function is returning what was given as parameter  i.e f(x) =x . and they  have told that in question 

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

now to backcheck it if we square and check it absolute value the value return would be what was passed

1
1
0 votes
0 votes
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