! This is a basic starting shell for your programs
MODULE const
INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0)
REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0
END MODULE const
!
PROGRAM prog
USE const
IMPLICIT NONE
!
! Program for HW 31, Problem 2
!
INTEGER :: n,k
REAL(kind=dp) :: answer
REAL(kind=dp) :: bincoeff
! NOTE: factfun is not directly seen by the main program, so it does not
! need to be declared here
! The only thing the main program should do is to read
! in the values of n and k, send these to the
! bincoeff function and print the result
WRITE(*,*) 'Input n,k'
READ(*,*) n,k
answer = bincoeff(n,k)
WRITE(*,*) 'bincoeff = ',answer
END PROGRAM prog
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION bincoeff(n,k)
USE const
IMPLICIT NONE
INTEGER,INTENT(IN) :: n,k
REAL(kind=dp) :: bincoeff
!
! Function to evaluate the binomial coefficient n choose k.
!
! This formula requires 3 factorial evaluations. We already
! have a function that will evaluate n! for any value of n.
!
! This factorial function will need to be called 3 times
! (once with an input of n, once with an input of k and once
! with an input of n-k).
REAL(kind=dp) :: factfun
REAL(kind=dp) :: v1,v2,v3
v1 = factfun(n)
v2 = factfun(k)
! NOTE: n-k can be sent directly as an arguement to the factorial
! function.
v3 = factfun(n-k)
bincoeff = v1/(v2*v3)
! NOTE: This could also be computed directly using
! bincoeff = factfun(n)/(factfun(k)*factfun(n-k))
RETURN
END FUNCTION bincoeff
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
FUNCTION factfun(n)
USE const
IMPLICIT NONE
!
! Function to compute n! given the value of n.
!
INTEGER,INTENT(IN) :: n
REAL(kind=dp) :: factfun
INTEGER :: i
factfun = 1
DO i = 1,n
factfun = factfun*i
ENDDO
END FUNCTION factfun
! Input n,k
! 14,5
! bincoeff = 2002.0000000000000