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
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
At line 8 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x799c773bf960 in ??? #1 0x799c773c04d9 in ??? #2 0x799c7761417b in ??? #3 0x799c7760d684 in ??? #4 0x799c7760e2aa in ??? #5 0x55a708272282 in MAIN__ #6 0x55a70827257c in main
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
temp.f95:24:4: 24 | n = n - 1 | 1 Error: Dummy argument ‘n’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:11:56: 11 | fib = fibonacci_recursive(n-1) + fibonacci_recursive(n-2) | 1 Error: Function ‘fibonacci_recursive’ at (1) cannot be called recursively, as it is not RECURSIVE temp.f95:31:5: 31 | use fibonacci | 1 Fatal Error: Cannot open module file ‘fibonacci.mod’ for reading at (1): No such file or directory compilation terminated.
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).
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).