Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active December 8, 2021 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/89e3b2b6cf2ef3ec3251168c018186b1 to your computer and use it in GitHub Desktop.
Save komasaru/89e3b2b6cf2ef3ec3251168c018186b1 to your computer and use it in GitHub Desktop.
Fortran 95 source code to compute the Fibonacci-series.
!****************************************************
! フィボナッチ数列計算
!
! DATE AUTHOR VERSION
! 2018.08.20 mk-mode.com 1.00 新規作成
! 2021.12.07 mk-mode.com 1.01 recursive を削除
!
! Copyright(C) 2018-2021 mk-mode.com All Rights Reserved.
!****************************************************
!
program fibonacci_main
implicit none
print *, fibonacci(0, 1, 20)
stop
contains
! フィボナッチ数列の計算
! * 初項 f1, f2, 長さ n のフィボナッチ数列を返す
!
! :param(in) integer f1
! :param(in) integer f2
! :param(in) integer n
! :return integer fibonacci(1:n)
function fibonacci(f1, f2, n)
implicit none
integer, intent(in) :: f1, f2, n
integer :: fibonacci(1:n)
integer :: i
fibonacci(1) = f1
fibonacci(2) = f2
do i = 3, n
fibonacci(i) = fibonacci(i - 1) + fibonacci(i - 2)
end do
end function fibonacci
end program fibonacci_main
@4e1e0603
Copy link

4e1e0603 commented Dec 7, 2021

Hi, thank you for your example. I have a question. In my opinion, this is an iterative version. You return the array with the same name as a function. If you remove the recursive keyword, the function still works. In other words, the fibonacci(i) is an array call... not a recursive function call.

@komasaru
Copy link
Author

komasaru commented Dec 8, 2021

Thank you for letting me know.
I don't remember clearly because it was a long time ago.
But certainly "recursive" wasn't needed.
I removed "recursive".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment