Question

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 qq (to 2 decimals) such that f(q)f(q) will return qq: _____.

Official answer and solution
Accepted answers
1.72 to 1.74
From
Official GATE 2014 answer key

Solution

Identify the base termination condition under which the recursive function returns immediately without recursive calls.

  1. Code Analysis:

    double f(double x){
        if( abs(x*x - 3) < 0.01) return x;
        else return f(x/2 + 1.5/x);
    }
    
  2. Condition for f(q)=qf(q) = q:

    • For f(q)f(q) to return qq immediately (or as a fixed point), the initial value qq must satisfy the base condition:
      ∣q2−3∣<0.01.|q^2 - 3| < 0.01.
    • This inequality translates to:
      −0.01<q2−3<0.01  ⟺  2.99<q2<3.01.-0.01 < q^2 - 3 < 0.01 \iff 2.99 < q^2 < 3.01.
  3. Evaluating qq:

    • Taking positive roots:
      2.99≈1.72916,3.01≈1.73493.\sqrt{2.99} \approx 1.72916, \qquad \sqrt{3.01} \approx 1.73493.
    • Since 3≈1.73205\sqrt{3} \approx 1.73205, setting q=1.73q = 1.73 satisfies:
      1.732=2.9929  ⟹  ∣2.9929−3∣=0.0071<0.01.1.73^2 = 2.9929 \implies |2.9929 - 3| = 0.0071 < 0.01.
    • Thus, f(1.73)f(1.73) immediately returns 1.731.73.
    • Any value qq in the range [1.72,1.74][1.72, 1.74] (or [−1.74,−1.72][-1.74, -1.72]) is accepted by the official answer key.
Final answer

1.73

Common trap

Trying to solve for convergence of all iterations rather than observing that the base case returns q directly when |q^2 - 3| < 0.01.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 2
Question
50 of 65, printed as CS Q.40
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.