### Computing Fibonacci numbers using recursion

• Fibonacci Numbers

• Around the year 1200 AD, Fibonacci considers the growth of an idealized (biologically unrealistic) rabbit population

• Assumptions:

 One newly born pair of rabbits, one male, one female, are put in a field (f0 = 1) Baby rabbits become adult rabbits in one month time. So they are able to mate at an age of one month An mating pair (only adult rabbits can mate) always produces one new pair (one male, one female) every month Rabbits never die...

• Fibonacci derived that under the above assumption, the number of pairs of rabbits can be expressed as:

 ``` Define: fn = # pairs of rabbits in after n month f0 = 1 (we put out 1 pair of baby rabbits in the field) f1 = 1 (after 1 month, we have 1 pair of adult rabbits) f2 = 1 + 1 = 2 (The adult pair got 1 baby pair) f3 = 2 + 1 = 3 (Babies become adults and 1 adult pair got 1 baby pair) f4 = 3 + 2 = 5 (Babies become adults and 2 adult pairs got 2 baby pairs) f5 = 5 + 3 = 8 (Babies become adults and 3 adult pairs got 3 baby pairs) f6 = 8 + 5 = 13 (Babies become adults and 5 adult pairs got 5 baby pairs) In general: fn = fn-1 + fn-2 for n ≥ 2 f0 = 1 f1 = 1 ```

• Fibonacci Numbers as a Java Method

• Problem description:

• Write a method to computer the Fibonacci number fn

• In other words, the method should have the following header:

 ``` public static int fibonacci( int n ) { .... } ```

The input parameter n is an integer

The output value (= fn) is also an integer value

• Divide and conquer the Fibonacci problem

• From above discussion:

 ``` fn = fn-1 + fn-2 with: f0 = 1 f1 = 1 ```

• The Fibonacci number problem has the recursive property

• The problem fibonacci(n) (= fn) can be solved using the solution of two smaller problem:

 fibonacci( n−1 ) (= fn-1) fibonacci( n−2 ) (= fn-2)

• The base (simple) cases n = 0 and n = 1 of the Fibonacci problem can be solved readily:

 ``` fibonacci(0) = 1 fibonacci(1) = 1 ```

• Therefore, we can apply the recursive algorithm technique to compute fibonacci(n):

• Which smaller problem do we use to solve fibonacci(n):

 ``` Since: fn = fn-1 + fn-2 We need: sol1 = fibonacci(n-1) sol2 = fibonacci(n-2) To compute: fibonacci(n) ```

• How do we use the solution sol1 to solve fibonacci(n):

 ``` fibonacci(n) = sol1 + sol2 ```

• Because we used fibonacci(n−2), we will need the solution for 2 base cases:

 ``` fibonacci(0) = 1 fibonacci(1) = 1 ```

• The recursive algorithm to solve fibonacci(n):

 ``` int fibonacci( int n ) { if ( n == 0 /* a base case */ ) { return 1; // The readily available solution for this base case } else if ( n == 1 /* another base case */ ) { return 1; // The readily available solution for this base case } else { /* --------------------------------------------- Let someone else solve the smaller problems fibonacci(n-1) and fibonacci(n-2) --------------------------------------------- */ sol1 = fibonacci ( n−1 ); sol2 = fibonacci ( n−2 ); /* --------------------------------------- Use the solutions sol1 and sol2 to solve my own problem: --------------------------------------- */ mySol = sol1 + sol2; return mySol; } } ```

• The algorithm written in Java:

 ``` public class Recurse2 { public static int fibonacci( int n ) { int sol1, sol2, solution; // Define variable... if ( n == 0 /* the first base case */ ) { return 1; // The readily available solution for this base case } else if ( n == 1 /* the second base case */ ) { return 1; // The readily available solution for this base case } else { sol1 = fibonacci ( n-1 ); // Hire "fibonacci" to solve first smaller problem sol2 = fibonacci ( n-2 ); // Hire "fibonacci" to solve second smaller problem mySol = sol1 + sol2; // Use the solutions of the smaller problems // to solve my own problem return mySol; } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac Recurse2.java To run:          java Recurse2

• Caveat of using Recursive Methods

• Caveat using recursive methods:

• The amount of memory used for parameter and local variables on the system stack can be very substantial !!!

(Fortunately, today's computer has a huge amount of memory.

Still, recursion can be very taxing on computing resources)

• The running time of recursive methods can be very long

 Try computing Fibonacci(20) (still OK) And try computing Fibonacci(50) (will run for a very long time)

• In a later course, you will learn why the Fibonacci recursion is so slow

And you will learn the dynamic programming technique to make it run faster.