function value = binom ( n, k )

%*****************************************************************************80
%
%% binom() computes the binomial coefficient.
%
%  Discussion:
%
%    This is ACM algorithm 160 translated.
%
%    It calculates the number of combinations of N things taken K at a time.
%
%  Modified:
%
%    30 March 2016
%
%  Author:
%
%    Bill Buckles, Matthew Lybanon
%
%  Reference:
%
%    Bill Buckles, Matthew Lybanon,
%    Algorithm 515: Generation of a Vector from the Lexicographical Index,
%    ACM Transactions on Mathematical Software,
%    Volume 3, Number 2, June 1977, pages 180-182.
%
%  Input:
%
%    integer N, K, the parameters for the binomial coefficient.
%
%  Output:
%
%    integer VALUE, the binomial coefficient.
%
  k1 = k;
  p = n - k1;

  if ( k1 < p )
    p = k1;
    k1 = n - p;
  end

  if ( p == 0 )
    r = 1;
  else
    r = k1 + 1;
  end

  for i = 2 : p
    r = floor ( ( r * ( k1 + i ) ) / i );
  end

  value = r;

  return
end
