Factorial and Combinatorial Coefficient

Problem Statement

The combinatorial coefficient C(n,r) is defined as follows:

where 0 <= r <= n must hold. Write a module that contains two functions: (1) Factorial() and (2) Combinatorial(). The former computes the factorial of its argument, while the latter uses the former to compute the combinatorial coefficient. Then, write a main program that uses this module.

Solution

The following is the desired module:
! --------------------------------------------------------------------
! MODULE  FactorialModule
!    This module contains two procedures: Factorial(n) and
! Combinatorial(n,r).  The first computes the factorial of an integer
! n and the second computes the combinatorial coefficient of two
! integers n and r.
! --------------------------------------------------------------------

MODULE  FactorialModule
   IMPLICIT  NONE

CONTAINS

! --------------------------------------------------------------------
! FUNCTION  Factorial() :
!    This function accepts a non-negative integers and returns its
! Factorial.
! --------------------------------------------------------------------

   INTEGER FUNCTION  Factorial(n)
      IMPLICIT  NONE

      INTEGER, INTENT(IN) :: n          ! the argument
      INTEGER             :: Fact, i    ! result

      Fact = 1                          ! initially, n!=1
      DO i = 1, n                       ! this loop multiplies
         Fact = Fact * i                ! i to n!
      END DO
      Factorial = Fact

   END FUNCTION  Factorial

! --------------------------------------------------------------------
! FUNCTION  Combinarotial():
!    This function computes the combinatorial coefficient C(n,r).
! If 0 <= r <= n, this function returns C(n,r), which is computed as
! C(n,r) = n!/(r!*(n-r)!).  Otherwise, it returns 0, indicating an
! error has occurred.
! --------------------------------------------------------------------

   INTEGER FUNCTION  Combinatorial(n, r)
      IMPLICIT  NONE

      INTEGER, INTENT(IN) :: n, r
      INTEGER             :: Cnr

      IF (0 <= r .AND. r <= n) THEN     ! valid arguments ?
         Cnr = Factorial(n) / (Factorial(r)*Factorial(n-r))
      ELSE                              ! no,
         Cnr = 0                        ! zero is returned
      END IF
      Combinatorial = Cnr

   END FUNCTION  Combinatorial

END MODULE  FactorialModule
Click here to download this program.

Here is the main program:

! --------------------------------------------------------------------
! PROGRAM  ComputeFactorial:
!    This program uses MODULE FactorialModule for computing factorial
! and combinatorial coefficients.
! --------------------------------------------------------------------

PROGRAM  ComputeFactorial
   USE       FactorialModule            ! use a module

   IMPLICIT  NONE

   INTEGER :: N, R

   WRITE(*,*)  'Two non-negative integers --> '
   READ(*,*)   N, R

   WRITE(*,*)  N,   '! = ', Factorial(N)
   WRITE(*,*)  R,   '! = ', Factorial(R)

   IF (R <= N) THEN                     ! if r <= n, do C(n,r)
      WRITE(*,*)  'C(', N, ',', R, ') = ', Combinatorial(N, R)
   ELSE                                 ! otherwise, do C(r,n)
      WRITE(*,*)  'C(', R, ',', N, ') = ', Combinatorial(R, N)
   END IF

END PROGRAM  ComputeFactorial
Click here to download this program.

Program Input and Output

The following is the output from the above program.
Two non-negative integers -->
13  4
13! = 1932053504
4! = 24
C(13,4) = 221

Discussion