Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Python

Idiom #301 Recursive Fibonacci sequence

Compute the Fibonacci sequence of n numbers using recursion.

Note that naive recursion is extremely inefficient for this task.

def fib(n):
    if n < 0:
        raise ValueError
    if n < 2:
        return n
    return fib(n - 2) + fib(n - 1)
        
int fibonacci(int n) => n <= 2 ? 1 : fibonacci(n - 2) + fibonacci(n - 1);

New implementation...
< >
Sikon