Fibonacci Number

🏠 ⬅️ ➡️

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1.

Given n, calculate F(n).

Example 1:

Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.

Example 3:

Input: n = 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Constraints:

  • 0 <= n <= 30

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program fibonacci
    implicit none

    integer :: n, i, j
    integer, dimension(:), allocatable :: fib

    do while ( .true. )
        read(*,*) n
        if ( n == 0 ) exit

        allocate(fib(n))
        fib(0) = 0
        fib(1) = 1
        do i=2,n
            fib(i) = fib(i-1) + fib(i-2)
        end do
        write(*,*) fib(n)
        deallocate(fib)
    end do
end program fibonacci
Compiled
Executed
Correct
module fibonacci
implicit none
contains

function fibonacci_recursive(n) result(fib)
integer, intent(in) :: n
integer :: fib
if (n == 0 .or. n == 1) then
    fib = n
else
    fib = fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
end if
end function fibonacci_recursive

function fibonacci_iterative(n) result(fib)
integer, intent(in) :: n
integer :: fib, a, b
a = 0
b = 1
do while (n > 0)
    fib = a + b
    a = b
    b = fib
    n = n - 1
end do
end function fibonacci_iterative

end module fibonacci

program main
use fibonacci
implicit none
integer :: n

n = 2
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 3
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 4
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 5
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 6
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 7
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 8
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 9
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 10
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 11
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 12
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 13
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 14
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 15
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 16
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 17
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 18
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 19
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 20
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 21
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 22
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 23
write (*,*) "Fibonacci number for n = ", n, " is ", fibonacci_recursive(n)

n = 24
write (*,*) "Fibonacci number for n = ", n
🌐 Data from online sources
def fib(n: int) -> int:
    if n < 2:
        return n
    a, b = 0, 1
    for _ in range(2, n + 1):
        a, b = b, a + b
    return b

The algorithm is an iterative approach to calculate the Fibonacci numbers. Instead of using the recursive formula, we maintain two variables a and b initialized with F(0) and F(1) respectively. On each iteration, we set c = a + b, then update a with the value of b and b with the value of c. This process increments both the Fibonacci numbers n times and the final result is stored in the variable b. This reduces the time complexity of the problem from exponential (recursive) to linear (iterative).

🌐 Data from online sources
int fib(int n) {
    if(n < 2) {
        return n;
    }
    int a = 0, b = 1, c;
    for(int i = 2; i <= n; ++i) {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}

The algorithm is an iterative approach to calculate the Fibonacci numbers. Instead of using the recursive formula, we maintain two variables a and b initialized with F(0) and F(1) respectively. On each iteration, we set c = a + b, then update a with the value of b and b with the value of c. This process increments both the Fibonacci numbers n times and the final result is stored in the variable b. This reduces the time complexity of the problem from exponential (recursive) to linear (iterative).