function value = i4_choose ( n, k )

%*****************************************************************************80
%
%% i4_choose() computes the binomial coefficient C(N,K) as an I4.
%
%  Discussion:
%
%    The value is calculated in such a way as to avoid overflow and
%    roundoff.  The calculation is done in integer arithmetic.
%
%    The formula used is:
%
%      C(N,K) = N! / ( K! * (N-K)! )
%
%    The MATLAB function nchoosek(n,k) should be preferred.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 January 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    ML Wolfson, HV Wright,
%    Algorithm 160:
%    Combinatorial of M Things Taken N at a Time,
%    Communications of the ACM,
%    Volume 6, Number 4, April 1963, page 161.
%
%  Input:
%
%    integer N, K, are the values of N and K.
%
%  Output:
%
%    integer VALUE, the number of combinations of N
%    things taken K at a time.
%
  mn = min ( k, n - k );
  mx = max ( k, n - k );

  if ( mn < 0 )

    value = 0;

  elseif ( mn == 0 )

    value = 1;

  else

    value = mx + 1;

    for i = 2 : mn
      value = ( value * ( mx + i ) ) / i;
    end

  end

  return
end
