How to resolve the algorithm Catalan numbers step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Catalan numbers step by step in the Fortran programming language
Table of Contents
Problem Statement
Catalan numbers are a sequence of numbers which can be defined directly: Or recursively: Or alternatively (also recursive):
Implement at least one of these algorithms and print out the first 15 Catalan numbers with each. Memoization is not required, but may be worth the effort when using the second method above.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Catalan numbers step by step in the Fortran programming language
Source code in the fortran programming language
program main
!=======================================================================================
implicit none
!=== Local data
integer :: n
!=== External procedures
double precision, external :: catalan_numbers
!=== Execution =========================================================================
write(*,'(1x,a)')'==============='
write(*,'(5x,a,6x,a)')'n','c(n)'
write(*,'(1x,a)')'---------------'
do n = 0, 14
write(*,'(1x,i5,i10)') n, int(catalan_numbers(n))
enddo
write(*,'(1x,a)')'==============='
!=======================================================================================
end program main
!BL
!BL
!BL
double precision recursive function catalan_numbers(n) result(value)
!=======================================================================================
implicit none
!=== Input, ouput data
integer, intent(in) :: n
!=== Execution =========================================================================
if ( n .eq. 0 ) then
value = 1
else
value = ( 2.0d0 * dfloat(2 * n - 1) / dfloat( n + 1 ) ) * catalan_numbers(n-1)
endif
!=======================================================================================
end function catalan_numbers
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Aime programming language
You may also check:How to resolve the algorithm Singular value decomposition step by step in the C++ programming language
You may also check:How to resolve the algorithm Thue-Morse step by step in the Nim programming language