@stdlib/math-base-special-binomcoef
v0.3.1
Published
Compute the binomial coefficient.
Downloads
117,493
Readme
Binomial Coefficient
Compute the binomial coefficient.
The binomial coefficient of two nonnegative integers n and k is defined as
The binomial coefficient can be generalized to negative integers n as follows:
Installation
npm install @stdlib/math-base-special-binomcoefUsage
var binomcoef = require( '@stdlib/math-base-special-binomcoef' );binomcoef( n, k )
Evaluates the binomial coefficient of two integers n and k.
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 NaNFor negative k, the function returns 0.
var v = binomcoef( 2, -1 );
// returns 0
v = binomcoef( -3, -1 );
// returns 0The function returns NaN for non-integer n or k.
var v = binomcoef( 2, 1.5 );
// returns NaN
v = binomcoef( 5.5, 2 );
// returns NaNExamples
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 APIs
Usage
#include "stdlib/math/base/special/binomcoef.h"stdlib_base_binomcoef( n, k )
Evaluates the binomial coefficient of two integers n and k.
double v = stdlib_base_binomcoef( 8.0, 2.0 );
// returns 28.0The function accepts the following arguments:
- n:
[in] doubleinput value. - k:
[in] doubleinput value.
double stdlib_base_binomcoef( const double n, const double k );Examples
#include "stdlib/math/base/special/binomcoef.h"
#include <stdio.h>
int main( void ) {
const double a[] = { 24.0, 32.0, 48.0, 116.0, 33.0 };
const double b[] = { 12.0, 6.0, 15.0, 52.0, 22.0 };
double out;
int i;
for ( i = 0; i < 5; i++ ) {
out = stdlib_base_binomcoef( a[ i ], b[ i ] );
printf( "binomcoef(%lf, %lf) = %lf\n", a[ i ], b[ i ], out );
}
}Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
