CSSE 221: Fibonacci, Tail-Recursion and Efficiency

In this exercise, you will learn about recursion and tail-recursion by implementing regular and tail-recursive versions of the fibonacci function. You will explore the difference in running time of the tail-recursive and recursive versions.

  1. Create a new Eclipse project and name it Fibonacci.
  2. Use Team → Share Project to attach your Fibonacci project to your individual SVN repository for this course.
  3. The mathematical function Fibonacci is defined as follows: definition of the Fibonacci function
  4. Part 1: Standard Fibonacci is inefficient

  5. Implement a recursive version of fib which returns the nth Fibonacci number.
  6. Write a main method that runs your method for n = 10, 20, 30, 40, and 50 and prints well-labeled output. Here are the expected answers:
  7. Modify your code to count the number of recursive calls used to calculate each value. You'll probably want to use a static field to store the count. Here are some expected answers, though depending on your implementation the number of steps might vary:
  8. Part 2: Tail-recusive Fibonacci is much more efficient!

  9. Now manually calculate and write down the first ten numbers in the Fibonacci sequence.
  10. Implement a tail-recursive version of the fibonacci function which returns the nth Fibonacci number. Keep these hints in mind:
  11. Test your tail-recursive version for n = 10, 20, 30, 40, 50, 100, and 1000.
  12. Modify your code to count the number of recursive calls used to calculate each value. You'll probably want to use a static field to store the count. Here are some expected answers, though depending on your implementation, the number of steps might vary:
  13. Commit your changes to be graded.