time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3903B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # Binomial Coefficient
     22 
     23 > Compute the [binomial coefficient][binomial-coefficient].
     24 
     25 <section class="intro">
     26 
     27 The [binomial coefficient][binomial-coefficient] of two nonnegative integers `n` and `k` is defined as
     28 
     29 <!-- <equation class="equation" label="eq:binomial_coefficient" align="center" raw="\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n" alt="Factorial formula for the Binomial coefficient."> -->
     30 
     31 <div class="equation" align="center" data-raw-text="\binom {n}{k} = \frac{n!}{k!\,(n-k)!} \quad \text{for }\ 0\leq k\leq n" data-equation="eq:binomial_coefficient">
     32     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@bb29798906e119fcb2af99e94b60407a270c9b32/lib/node_modules/@stdlib/math/base/special/binomcoef/docs/img/equation_binomial_coefficient.svg" alt="Factorial formula for the Binomial coefficient.">
     33     <br>
     34 </div>
     35 
     36 <!-- </equation> -->
     37 
     38 The [binomial coefficient][binomial-coefficient] can be generalized to negative integers `n` as follows:
     39 
     40 <!-- <equation class="equation" label="eq:binomial_coefficient_negative_integers" align="center" raw="\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)" alt="Generalization of the binomial coefficient to negative n."> -->
     41 
     42 <div class="equation" align="center" data-raw-text="\binom {-n}{k} = (-1)^{k} \binom{n + k - 1}{k} = (-1)^{k} \left(\!\!{\binom {n}{k}}\!\!\right)" data-equation="eq:binomial_coefficient_negative_integers">
     43     <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@591cf9d5c3a0cd3c1ceec961e5c49d73a68374cb/lib/node_modules/@stdlib/math/base/special/binomcoef/docs/img/equation_binomial_coefficient_negative_integers.svg" alt="Generalization of the binomial coefficient to negative n.">
     44     <br>
     45 </div>
     46 
     47 <!-- </equation> -->
     48 
     49 </section>
     50 
     51 <!-- /.intro -->
     52 
     53 <section class="usage">
     54 
     55 ## Usage
     56 
     57 ```javascript
     58 var binomcoef = require( '@stdlib/math/base/special/binomcoef' );
     59 ```
     60 
     61 #### binomcoef( n, k )
     62 
     63 Evaluates the [binomial coefficient][binomial-coefficient] of two integers `n` and `k`.
     64 
     65 ```javascript
     66 var v = binomcoef( 8, 2 );
     67 // returns 28
     68 
     69 v = binomcoef( 0, 0 );
     70 // returns 1
     71 
     72 v = binomcoef( -4, 2 );
     73 // returns 10
     74 
     75 v = binomcoef( 5, 3 );
     76 // returns 10
     77 
     78 v = binomcoef( NaN, 3 );
     79 // returns NaN
     80 
     81 v = binomcoef( 5, NaN );
     82 // returns NaN
     83 
     84 v = binomcoef( NaN, NaN );
     85 // returns NaN
     86 ```
     87 
     88 For negative `k`, the function returns `0`.
     89 
     90 ```javascript
     91 var v = binomcoef( 2, -1 );
     92 // returns 0
     93 
     94 v = binomcoef( -3, -1 );
     95 // returns 0
     96 ```
     97 
     98 The function returns `NaN` for non-integer `n` or `k`.
     99 
    100 ```javascript
    101 var v = binomcoef( 2, 1.5 );
    102 // returns NaN
    103 
    104 v = binomcoef( 5.5, 2 );
    105 // returns NaN
    106 ```
    107 
    108 </section>
    109 
    110 <!-- /.usage -->
    111 
    112 <section class="examples">
    113 
    114 ## Examples
    115 
    116 <!-- eslint no-undef: "error" -->
    117 
    118 ```javascript
    119 var randu = require( '@stdlib/random/base/randu' );
    120 var round = require( '@stdlib/math/base/special/round' );
    121 var binomcoef = require( '@stdlib/math/base/special/binomcoef' );
    122 
    123 var n;
    124 var k;
    125 var i;
    126 
    127 for ( i = 0; i < 100; i++ ) {
    128     n = round( (randu()*30.0) - 10.0 );
    129     k = round( randu()*20.0 );
    130     console.log( '%d choose %d = %d', n, k, binomcoef( n, k ) );
    131 }
    132 ```
    133 
    134 </section>
    135 
    136 <!-- /.examples -->
    137 
    138 <section class="links">
    139 
    140 [binomial-coefficient]: https://en.wikipedia.org/wiki/Binomial_coefficient
    141 
    142 </section>
    143 
    144 <!-- /.links -->