Computes the binomial coefficient with the given arguments.

View versions (1)

Interface

#include <codecogs/maths/combinatorics/arithmetic/binomial_coefficient_gamma.h>

using namespace Maths::Combinatorics::Arithmetic;

This function calculates the binomial coefficient C(n, k) directly, using the logarithm of the Gamma function, rather than recursively. C(n, k) is the number of distinct combinations of k objects chosen from a set of n distinct objects. A combination is like a set, in that order does not matter. The computed expression has the following form:

C(n, k) = \left( \begin{array}{c} n \cr k \end{array} \right)
        = \frac{n!}{k!(n - k)!}
(1)

This coefficient represents the number of distinct combinations of k objects chosen from a set of n distinct objects. The advantage in using this function is that large values of n and k may be given, without the fear of arithmetic overflow during the intermediate calculations. This is possible with the use of the logarithm of the Gamma function.

Example:

#include <codecogs/maths/combinatorics/arithmetic/binomial_coefficient_gamma.h>
#include <iostream>
int main()
{
  for (int i = 0; i <= 6; i++)
  {
    std::cout << "C(6, " << i << ") = ";
    std::cout << Maths::Combinatorics::Arithmetic::binomial_coefficient_gamma(6, i);
    std::cout << std::endl;
  }
  return 0;
}

Output:

C(6, 0) = 1
C(6, 1) = 6
C(6, 2) = 15
C(6, 3) = 20
C(6, 4) = 15
C(6, 5) = 6
C(6, 6) = 1

References

SUBSET, a C++ library of combinatorial routines, http://www.csit.fsu.edu/~burkardt/cpp_src/subset/subset.html

Parameters

n
the first combinatorial parameter
k
the second combinatorial parameter

Returns

the binomial coefficients with parameters n and k
GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

n
k
Result