-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfibonacci.f90
50 lines (46 loc) · 1.39 KB
/
fibonacci.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
!> Module for Fibonacci series calculations
!!
!! Modified by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #31
!! https://github.com/TheAlgorithms/Fortran/pull/31
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! Provides both recursive and iterative implementations.
!! Reference: https://en.wikipedia.org/wiki/Fibonacci_number
module fibonacci_module
implicit none
contains
!! Recursive function to compute the nth Fibonacci number
recursive function fib_rec(n) result(f)
integer, intent(in), value :: n
integer :: f
if (n < 0) then
f = -1 ! signal error condition
else if (n <= 1) then
f = n
else
f = fib_rec(n - 1) + fib_rec(n - 2)
end if
end function fib_rec
!! Iterative function to compute the nth Fibonacci number
function fib_itr(n) result(f)
integer, intent(in) :: n
integer :: f, tmp, f_1
integer :: i
if (n < 0) then
f = -1 ! signal error condition
else if (n <= 1) then
f = n
else
f_1 = 0
f = 1
do i = 2, n
tmp = f
f = f + f_1
f_1 = tmp
end do
end if
end function fib_itr
end module fibonacci_module