ncr(3f) - [M_math] Calculate the number of unique combinations of r objects out of n.
Synopsis
Description
Options
Example
Author
subroutine ncr(n,r,ncomb,ier)
integer, intent(in) :: n integer, intent(in) :: r !*!integer, parameter :: dp = selected_real_kind(12, 60) integer, parameter :: dp = kind(0.0d0) real (dp), intent(out) :: ncomb integer, intent(out) :: ier
Calculate the number of unique combinations of r objects out of n.
n number of objects r number of the objects to select in a set ncomp returns number of unique combinations ier returns error code
o 0 if no error is detected o 1 if n < 1 o 2 if r < 0 o 3 if r > n o 4 if nCr > 1.e+308, i.e. if it overflows. In this case, the natural log of nCr is returned.
Sample program:
program demo_ncr use m_math, only : ncr implicit none integer, parameter :: dp = selected_real_kind(12, 60) integer :: n, r, ier real (dp) :: result !do ! write(*, (a), advance=no) Enter n, r : ! read(*, *) n, r n=10 r=2 call ncr(n, r, result, ier) if (ier /= 0) then write(*, *) Error, IER = , ier if (ier == 4) write(*, (a, f12.5)) ln(ncr) = , result else write(*, (a, g16.8)) ncr = , result endif !enddo end program demo_ncrResults:
ncr = 45.000000
Alan Miller
Nemo Release 3.1 | ncr (3) | February 23, 2025 |