Skip to content

GATE CS 2003 Q1 — Asymptotic Worst Case Time and Space Complexity

Explain with AI

Consider the following CC function.

For large values of yy, the return value of the function ff best approximates

float f,(float x, int y) {
    float p, s; int i;
    for (s=1,p=1,i=1; i<y; i++) {
        p *= x/i;
        s += p;
    }
    return s;
}

Choose one option
View worked solution

Answer: B

To find what the function f(x,y)f(x, y) approximates for large values of yy, let's trace the values of the variables pp and ss inside the loop.

Initially, before the loop starts:

  • s=1s = 1
  • p=1p = 1

The loop runs for i=1,2,3,…,y−1i = 1, 2, 3, \dots, y-1:

  • For i=1i = 1:
    p=p×x1=x1!p = p \times \frac{x}{1} = \frac{x}{1!}
    s=s+p=1+x1!s = s + p = 1 + \frac{x}{1!}
  • For i=2i = 2:
    p=p×x2=x22!p = p \times \frac{x}{2} = \frac{x^2}{2!}
    s=s+p=1+x1!+x22!s = s + p = 1 + \frac{x}{1!} + \frac{x^2}{2!}
  • For i=3i = 3:
    p=p×x3=x33!p = p \times \frac{x}{3} = \frac{x^3}{3!}
    s=s+p=1+x1!+x22!+x33!s = s + p = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!}

Continuing this process up to i=y−1i = y-1, the final value of ss returned by the function is:

s=1+x1!+x22!+x33!+⋯+xy−1(y−1)!s = 1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots + \frac{x^{y-1}}{(y-1)!}

For large values of yy (i.e., as y→∞y \to \infty), this infinite series is the standard Taylor series expansion of the exponential function:

ex=∑n=0∞xnn!=1+x+x22!+x33!+…e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!} = 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \dots

Therefore, the return value of the function best approximates exe^x.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Asymptotic Worst Case Time and Space Complexity questions