panthrMath/basicFunc/choose.js
function choose(n, k) {
      var ret, j;

      k = Math.round(k);
      if (utils.hasNaN(n, k)) { return NaN; }
      if (k < kSmallMax) {
         if (n - k < k && n >= 0 && n === Math.round(n)) { k = n - k; }
         if (k < 0) { return 0; }
         if (k === 0) { return 1; }
         ret = n;
         for (j = 2; j <= k; j += 1) {
            ret *= (n - j + 1) / j;
         }

         return n === Math.round(n) ? Math.round(ret) : ret;
      }
      if (n < 0) {
         ret = choose(-n + k - 1, k);

         return k % 2 === 1 ? -ret : ret;
      }
      if (n === Math.round(n)) {
         if (n < k) { return 0; }
         if (n - k < kSmallMax) { return choose(n, n - k); }

         return Math.round(Math.exp(lfastchoose(n, k)));
      }
      if (n < k - 1) {
         return signGamma(n - k + 1) * Math.exp(lfastchoose2(n, k));
      }

      return Math.exp(lfastchoose(n, k));
   }
function choose(n, k) {
      var ret, j;

      k = Math.round(k);
      if (utils.hasNaN(n, k)) { return NaN; }
      if (k < kSmallMax) {
         if (n - k < k && n >= 0 && n === Math.round(n)) { k = n - k; }
         if (k < 0) { return 0; }
         if (k === 0) { return 1; }
         ret = n;
         for (j = 2; j <= k; j += 1) {
            ret *= (n - j + 1) / j;
         }

         return n === Math.round(n) ? Math.round(ret) : ret;
      }
      if (n < 0) {
         ret = choose(-n + k - 1, k);

         return k % 2 === 1 ? -ret : ret;
      }
      if (n === Math.round(n)) {
         if (n < k) { return 0; }
         if (n - k < kSmallMax) { return choose(n, n - k); }

         return Math.round(Math.exp(lfastchoose(n, k)));
      }
      if (n < k - 1) {
         return signGamma(n - k + 1) * Math.exp(lfastchoose2(n, k));
      }

      return Math.exp(lfastchoose(n, k));
   }
function choose ( n , k ) { var ret , j ; k = Math . round ( k ) ; if ( utils . hasNaN ( n , k ) ) { return NaN ; } if ( k < kSmallMax ) { if ( n - k < k && n >= 0 && n === Math . round ( n ) ) { k = n - k ; } if ( k < 0 ) { return 0 ; } if ( k === 0 ) { return 1 ; } ret = n ; for ( j = 2 ; j <= k ; j += 1 ) { ret *= ( n - j + 1 ) / j ; } return n === Math . round ( n ) ? Math . round ( ret ) : ret ; } if ( n < 0 ) { ret = choose ( - n + k - 1 , k ) ; return k % 2 === 1 ? - ret : ret ; } if ( n === Math . round ( n ) ) { if ( n < k ) { return 0 ; } if ( n - k < kSmallMax ) { return choose ( n , n - k ) ; } return Math . round ( Math . exp ( lfastchoose ( n , k ) ) ) ; } if ( n < k - 1 ) { return signGamma ( n - k + 1 ) * Math . exp ( lfastchoose2 ( n , k ) ) ; } return Math . exp ( lfastchoose ( n , k ) ) ; }
Computes the binomial coefficient "n choose k".
Valid for any real n and for integer k.
k will be rounded if it is not an integer.
Computes the binomial coefficient n choose k . Valid for any real n and for integer k . k will be rounded if it is not an integer .