Calculates the logarithm of the binomial coefficient with given arguments.

View versions (1)

Interface

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

using namespace Maths::Combinatorics::Arithmetic;

This function calculates the following expression

\mathrm{log binomial}(n) = \mathrm{ln} \left(\begin{array}{c}n\\k \end{array} \right)
                         = \mathrm{ln}\,\left( \frac{n!}{k!(n-k)!} \right)
(1)

In the given example, values of this function are returned for various arguments. The algorithm is designed in such a way to permit large values of the arguments, without creating overflow errors during the calculations. The example also demonstrates this feature, with n equal to 100 and k taking values from 70 up to 80.

Example 1

#include <codecogs/maths/combinatorics/arithmetic/log_binomial.h>
#include <iostream>
#include <iomanip>

int main()
{
  std::cout << std::setprecision(10);
  for (int i = 70; i <= 80; i++)
  {
    std::cout << "log_binomial(100, " << i << ") = ";
    std::cout << Maths::Combinatorics::Arithmetic::log_binomial(100, i);
    std::cout << std::endl;
  }
  return 0;
}

Output

log_binomial(100, 70) = 58.64209564
log_binomial(100, 71) = 57.78061315
log_binomial(100, 72) = 56.87124286
log_binomial(100, 73) = 55.91298793
log_binomial(100, 74) = 54.9047597
log_binomial(100, 75) = 53.84536812
log_binomial(100, 76) = 52.73351061
log_binomial(100, 77) = 51.56775902
log_binomial(100, 78) = 50.34654441
log_binomial(100, 79) = 49.06813901
log_binomial(100, 80) = 47.73063481

Parameters

n
the first argument of the binomial coefficient (the number of objects)
k
the second argument of the binomial coefficient (the number of choices)

Returns

the logarithm of the binomial coefficient with given arguments n and k
GPL Licence — free for non commercial use. See Licence details.

Interactive Calculator

n
k
Result