• + 0 comments

    This is the basic introduction to the recurssion chapter and probably this problem is always first approached when someone tries to present this topic. As with every recursion problem you need to cover your base cases and end the recursive stack. For this problem, the base case is different than your usual Fibonacci problem, but should not be to hard to code. Here's how it would look like in Scala:

    def fibonacci(x:Int): Int = {
              if (x <= 1) 0;
              else if (x == 2) 1;
              else fibonacci(x-1) + fibonacci(x-2);
    }