Logo

Programming-Idioms

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

Idiom #301 Recursive Fibonacci sequence

Compute the Fibonacci sequence of n numbers using recursion.

Note that naive recursion is extremely inefficient for this task.

fn fib(n: i32) -> i32 {
    if n < 2 {
        1
    } else {
        fib(n - 2) + fib(n - 1)
    }
}
int fibonacci(int n) => n <= 2 ? 1 : fibonacci(n - 2) + fibonacci(n - 1);

New implementation...
< >
Sikon