CS141 Lab 3



Class Webpage:  http://www.cs.ucr.edu/~jiang/141-homepage.html


For the labs of the third week:

printFib(n): Given a number n print the nth Fibonacci number

    Do it in two ways:
1. Recursively using recurrence: fib(n) = fib(n-1) + fib(n-2)

2. Iteratively:

    printFib(n)
        x = 0
        y = 1
        for i = 2 to n
            z = x + y
            x = y
            y = z
        Print z
  • Implement both algorithms and count time difference as a function of n.  Create a table and observe the trend.
  • Obtain the asymptotic complexity of each algorithm as functions of n, using a recurrence relation for the recurisve one. For simplicity, approximate the recurrence relation with T(n) <= 2*T(n-1). Compare the complexity functions and correlate with the observed execution times.
    TA: Keep track of which students completed the exercise.