Consider the following function. double f(double x){ if( abs(x*x - 3) < 0.01)…

2014

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\):_____.

Attempted by 114 students.

Show answer & explanation

Correct answer: 1.73

Answer: q = 1.73

Explanation:

  • The function returns x immediately when the condition |x^2 - 3| < 0.01 holds.

  • Picking q = 1.73 (rounded to two decimals).

  • Compute q^2 = 1.73^2 = 2.9929, so |q^2 - 3| = |2.9929 - 3| = 0.0071, which is less than 0.01.

  • Therefore the base case is satisfied and f(1.73) returns 1.73.

  • Note: The negative value -1.73 also satisfies the inequality, so it would also be returned, but the answer above gives one valid value rounded to two decimals.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir