Compute the binomial coefficient.
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we’ve built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url]
Compute the [binomial coefficient][binomial-coefficient].
n
and k
is defined asmath
\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n
n
as follows:math
\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)
bash
npm install @stdlib/math-base-special-binomcoef
script
tag without installation and bundlers, use the [ES Module][es-module] available on the [esm
][esm-url] branch (see [README][esm-readme]).deno
][deno-url] branch (see [README][deno-readme] for usage intructions).umd
][umd-url] branch (see [README][umd-readme]).javascript
var binomcoef = require( '@stdlib/math-base-special-binomcoef' );
n
and k
.javascript
var v = binomcoef( 8, 2 );
// returns 28
v = binomcoef( 0, 0 );
// returns 1
v = binomcoef( -4, 2 );
// returns 10
v = binomcoef( 5, 3 );
// returns 10
v = binomcoef( NaN, 3 );
// returns NaN
v = binomcoef( 5, NaN );
// returns NaN
v = binomcoef( NaN, NaN );
// returns NaN
k
, the function returns 0
.javascript
var v = binomcoef( 2, -1 );
// returns 0
v = binomcoef( -3, -1 );
// returns 0
NaN
for non-integer n
or k
.javascript
var v = binomcoef( 2, 1.5 );
// returns NaN
v = binomcoef( 5.5, 2 );
// returns NaN
javascript
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var logEachMap = require( '@stdlib/console-log-each-map' );
var binomcoef = require( '@stdlib/math-base-special-binomcoef' );
var opts = {
'dtype': 'float64'
};
var n = discreteUniform( 100, -10, 30, opts );
var k = discreteUniform( 100, 0, 20, opts );
logEachMap( '%d choose %d = %d', n, k, binomcoef );
c
#include "stdlib/math/base/special/binomcoef.h"
n
and k
.c
double v = stdlib_base_binomcoef( 8, 2 );
// returns 28.0
[in] int64_t
input value.[in] int64_t
input value.c
double stdlib_base_binomcoef( const int64_t n, const int64_t k );
c
#include "stdlib/math/base/special/binomcoef.h"
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main( void ) {
const int64_t a[] = { 24, 32, 48, 116, 33 };
const int64_t b[] = { 12, 6, 15, 52, 22 };
double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_binomcoef( a[ i ], b[ i ] );
printf( "binomcoef(%" PRId64 ", %" PRId64 ") = %lf\n", a[ i ], b[ i ], out );
}
}