Image by Cool Text: Logo and Button Generator - Create Your Own

Wednesday, March 16, 2011


Bookmark and Share

Recursion & bad examples

If you ask a typical computer science graduate from Kerala to write a program to print the nth Fibonacci number, most of them* will invariably give you the following function:

int fibonacci (int n){
        return n;
        return fibonacci(n-1) + fibonacci(n-2);

 So far so good, except that the answer is wrong.

Recursion is the worst way to find a Fibonacci number. The last time I checked it was impossible to use recursion to compute even the 50th Fibonacci number in a personal computer!
If it is impossible to calculate even the 50th Fibonacci number using this function, how could you possibly teach something like this in a computer science course? The only way Fibonacci numbers should be calculated is by linearly adding the numbers in a loop or by using any direct formula you have. Of course for some applications you can speed up recursion by remembering the child nodes in the tree and thereby avoiding doing the same calculations again in some other branch.
The scariest part is yet to come. In many colleges they use finding the nth Fibonacci number as the primary example for teaching recursion!
Why not teach students the best possible way to find the nth Fibonacci number? Why not teach a real world example for recursion? Is it necessary to teach the concepts in computer science using lousy examples?

No comments:

Post a Comment