Evaluate binomial coefficients

From Rosetta Code
Revision as of 18:23, 13 November 2011 by Hakank (talk | contribs) (→‎{{header|K}})
Task
Evaluate binomial coefficients
You are encouraged to solve this task according to the task description, using any language you may know.

This programming task, is to calculate ANY binomial coefficient.

However, it has to be able to output , which is 10.

This formula is recommended:

Ada

<lang Ada> with Ada.Text_IO; use Ada.Text_IO; procedure Test_Binomial is

  function Binomial (N, K : Natural) return Natural is
     Result : Natural := 1;
     M      : Natural;
  begin
     if N < K then
        raise Constraint_Error;
     end if;
     if K > N/2 then -- Use symmetry
        M := N - K;
     else
        M := K;
     end if;
     for I in 1..M loop
        Result := Result * (N - M + I) / I;
     end loop;
     return Result;
  end Binomial;

begin

  for N in 0..17 loop
     for K in 0..N loop
        Put (Integer'Image (Binomial (N, K)));
     end loop;
     New_Line;
  end loop;

end Test_Binomial; </lang> Sample output:

 1
 1 1
 1 2 1
 1 3 3 1
 1 4 6 4 1
 1 5 10 10 5 1
 1 6 15 20 15 6 1
 1 7 21 35 35 21 7 1
 1 8 28 56 70 56 28 8 1
 1 9 36 84 126 126 84 36 9 1
 1 10 45 120 210 252 210 120 45 10 1
 1 11 55 165 330 462 462 330 165 55 11 1
 1 12 66 220 495 792 924 792 495 220 66 12 1
 1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
 1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
 1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
 1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
 1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1

ALGOL 68

Iterative - unoptimised

Translation of: C

- note: This specimen retains the original C coding style.

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d

<lang algol68>PROC factorial = (INT n)INT: (

       INT result;

       result := 1;
       FOR i  TO n DO
               result *:= i
       OD;

       result

);

PROC choose = (INT n, INT k)INT: (

       INT result;
  1. Note: code can be optimised here as k < n #
       result := factorial(n) OVER (factorial(k) * factorial(n - k));

       result

);

test:(

       print((choose(5, 3), new line))

)</lang> Output:

        +10

AppleScript

<lang AppleScript>set n to 5 set k to 3

on calculateFactorial(val) set partial_factorial to 1 as integer repeat with i from 1 to val set factorial to i * partial_factorial set partial_factorial to factorial end repeat return factorial end calculateFactorial

set n_factorial to calculateFactorial(n) set k_factorial to calculateFactorial(k) set n_minus_k_factorial to calculateFactorial(n - k)

return n_factorial / (n_minus_k_factorial) * 1 / (k_factorial) as integer </lang>

AutoHotkey

<lang autohotkey>MsgBox, % Round(BinomialCoefficient(5, 3))

---------------------------------------------------------------------------

BinomialCoefficient(n, k) {

---------------------------------------------------------------------------
   r := 1
   Loop, % k < n - k ? k : n - k {
       r *= n - A_Index + 1
       r /= A_Index
   }
   Return, r

}</lang> Message box shows:

10

C

<lang C>#include <stdio.h>

  1. include <limits.h>

typedef unsigned long ULONG;

ULONG binomial(ULONG n, ULONG k) { ULONG r = 1, d = n - k;

/* choose the smaller of k and n - k */ if (d > k) { k = d; d = n - k; }

while (n > k) { if (r >= UINT_MAX / n) return 0; /* overflown */ r *= n--;

/* divide (n - k)! as soon as we can to delay overflows */ while (d > 1 && !(r % d)) r /= d--; } return r; }

int main() { printf("%lu\n", binomial(5, 3)); return 0; }</lang> Output: <lang>10</lang>

C++

<lang cpp>double Factorial(double nValue)

  {
      double result = nValue;
      double result_next;
      double pc = nValue;
      do
      {
          result_next = result*(pc-1);
          result = result_next;
          pc--;
      }while(pc>2);
      nValue = result;
      return nValue;
  }

double EvaluateBinomialCoefficient(double nValue, double nValue2)

  {
      double result;
      if(nValue2 == 1)return nValue;
      result = (Factorial(nValue))/(Factorial(nValue2)*Factorial((nValue - nValue2)));
      nValue2 = result;
      return nValue2;
  }</lang>

Implementation: <lang cpp>int main() {

   cout<<"The Binomial Coefficient of 5, and 3, is equal to: "<< EvaluateBinomialCoefficient(5,3);
   cin.get();

}</lang>

Output:

The Binomial Coefficient of 5, and 3, is equal to: 10

C#

<lang csharp>using System;

namespace BinomialCoefficients {

   class Program
   {
       static void Main(string[] args)
       {
           ulong n = 1000000, k = 3;
           ulong result = biCoefficient(n, k);
           Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result);
           Console.ReadLine();
       }
       static int fact(int n)
       {
           if (n == 0) return 1;
           else return n * fact(n - 1);
       }
       static ulong biCoefficient(ulong n, ulong k)
       {
           if (k > n - k)
           {
               k = n - k;
           }
           ulong c = 1;
           for (uint i = 0; i < k; i++)
           {
               c = c * (n - i);
               c = c / (i + 1);
           }
           return c;
       }
   }

}</lang>

Clojure

<lang clojure>(defn binomial-coefficient [n k]

 (let [rprod (fn [a b] (reduce * (range a (inc b))))]
   (/ (rprod (- n k -1) n) (rprod 1 k))))</lang>

Common Lisp

<lang lisp> (defun choose (n k)

 (labels ((prod-enum (s e)

(do ((i s (1+ i)) (r 1 (* i r))) ((> i e) r))) (fact (n) (prod-enum 1 n)))

   (/ (prod-enum (- (1+ n) k) n) (fact k))))

</lang>

D

<lang d>import std.stdio, std.bigint;

T binomial(T)(/*in*/ T n, T k) /*pure nothrow*/ {

   if (k > n/2)
       k = n - k;
   T bc = n - k + 1;
   //foreach (i; 2 .. k+1)
   for (T i = 2; i <= k; i++)
       bc = (bc * (n - k + i)) / i;
   return bc;

}

void main() {

   foreach (d; [[5,3], [100,2], [100,98]])
       writefln("(%3d %3d) = %s", d[0], d[1], binomial(d[0], d[1]));
   writeln("(100  50) = ", binomial(BigInt(100), BigInt(50)));

}</lang> Output:

(  5   3) = 10
(100   2) = 4950
(100  98) = 4950
(100  50) = 100891344545564193334812497256

Delphi

<lang Delphi>program Binomial;

{$APPTYPE CONSOLE}

function BinomialCoff(N, K: Cardinal): Cardinal; var

 L: Cardinal;

begin

 if N < K then
   Result:= 0      // Error
 else begin
   if K > N - K then
     K:= N - K;    // Optimization
   Result:= 1;
   L:= 0;
   while L < K do begin
     Result:= Result * (N - L);
     Inc(L);
     Result:= Result div L;
   end;
 end;

end;

begin

 Writeln('C(5,3) is ', BinomialCoff(5, 3));
 ReadLn;

end.</lang>

Erlang

<lang erlang> choose(N, K) when is_integer(N), is_integer(K), (N >= 0), (K >= 0), (N >= K) ->

 choose(N, K, 1, 1).

choose(N, K, K, Acc) ->

 (Acc * (N-K+1)) div K;

choose(N, K, I, Acc) ->

 choose(N, K, I+1, (Acc * (N-I+1)) div I).

</lang>

F#

<lang fsharp> let factorial n = List.fold (*) 1 [1..n] let choose n k =

   (factorial n)/(factorial(n-k)*factorial(k))

</lang>

Forth

<lang forth>: choose ( n k -- nCk ) 1 swap 0 ?do over i - i 1+ */ loop nip ;

5  3 choose .   \ 10

33 17 choose . \ 1166803110</lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran>program test_choose

 implicit none
 write (*, '(i0)') choose (5, 3)

contains

 function factorial (n) result (res)
   implicit none
   integer, intent (in) :: n
   integer :: res
   integer :: i
   res = product ((/(i, i = 1, n)/))
 end function factorial
 function choose (n, k) result (res)
   implicit none
   integer, intent (in) :: n
   integer, intent (in) :: k
   integer :: res
   res = factorial (n) / (factorial (k) * factorial (n - k))
 end function choose

end program test_choose</lang> Output: <lang>10</lang>

Frink

Frink has a built-in efficient function to find binomial coefficients. It produces arbitrarily-large integers. <lang frink> println[binomial[5,3]] </lang>

GAP

<lang gap># Built-in Binomial(5, 3);

  1. 10</lang>

Go

<lang go>package main import "fmt" import "big"

func main() {

 fmt.Println(new(big.Int).Binomial(5, 3))

}</lang>

Golfscript

Actually evaluating n!/(k! (n-k)!): <lang golfscript>;5 3 # Set up demo input {),(;{*}*}:f; # Define a factorial function .f@.f@/\@-f/</lang> But Golfscript is meant for golfing, and it's shorter to calculate :

<lang golfscript>;5 3 # Set up demo input 1\,@{1$-@\*\)/}+/</lang>

Groovy

Solution: <lang groovy>def factorial = { x ->

   assert x > -1
   x == 0 ? 1 : (1..x).inject(1G) { BigInteger product, BigInteger factor -> product *= factor }

}

def combinations = { n, k ->

   assert k >= 0
   assert n >= k
   factorial(n).intdiv(factorial(k)*factorial(n-k))

}</lang>

Test: <lang groovy>assert combinations(20, 0) == combinations(20, 20) assert combinations(20, 10) == (combinations(19, 9) + combinations(19, 10)) assert combinations(5, 3) == 10 println combinations(5, 3)</lang>

Output:

10

Haskell

The only trick here is realizing that everything's going to divide nicely, so we can use div instead of (/).

<lang haskell> choose :: (Integral a) => a -> a -> a choose n k = product([k+1..n]) `div` product([1..n-k]) </lang>

<lang haskell>> 5 `choose` 3 10</lang>

HicEst

<lang HicEst>WRITE(Messagebox) BinomCoeff( 5, 3) ! displays 10

FUNCTION factorial( n )

  factorial = 1
  DO i = 1, n
     factorial = factorial * i
  ENDDO

END

FUNCTION BinomCoeff( n, k )

  BinomCoeff = factorial(n)/factorial(n-k)/factorial(k)

END</lang>

Icon and Unicon

<lang Icon>link math, factors

procedure main() write("choose(5,3)=",binocoef(5,3)) end</lang> Output:

choose(5,3)=10

math provides binocoef and factors provides factorial.

<lang Icon>procedure binocoef(n, k) #: binomial coefficient

  k := integer(k) | fail
  n := integer(n) | fail
  if (k = 0) | (n = k) then return 1
  if 0 <= k <= n then 
     return factorial(n) / (factorial(k) * factorial(n - k))
  else fail

end

procedure factorial(n) #: return n! (n factorial)

  local i
  n := integer(n) | runerr(101, n)
  if n < 0 then fail
  i := 1
  every i *:= 1 to n
  return i

end</lang>

J

Solution:
The dyadic form of the primitive ! ([Out of]) evaluates binomial coefficients directly.

Example usage: <lang j> 3 ! 5 10</lang>

Java

<lang java>public class Binom {

static long combinations(int n, int k) { long coeff = 1; for (int i = n - k + 1; i <= n; i++) { coeff *= i; } for (int i = 1; i <= k; i++) { coeff /= i; } return coeff; }

   public static void main(String[] args){
       System.out.println(combinations(5, 3));
   }

}</lang> Output:

10.0
Translation of: Python

<lang java>public class Binom {

   public static double binomCoeff(double n, double k) {
       double result = 1;
       for (int i = 1; i < k + 1; i++) {
           result *= (n - i + 1) / i;
       }
       return result;
   }
   public static void main(String[] args) {
       System.out.println(binomCoeff(5, 3));
   }

} </lang> Output:

10.0

JavaScript

<lang javascript>function binom(n, k) {

   var coeff = 1;
   for (var i = n-k+1; i <= n; i++) coeff *= i;
   for (var i = 1;     i <= k; i++) coeff /= i;
   return coeff;

} print(binom(5,3));</lang>

10

K

<lang K> {[n;k]_(*/(k-1)_1+!n)%(*/1+!k)} . 5 3 10</lang>

Alternative version: <lang K> {[n;k]i:!(k-1);_*/((n-i)%(i+1))} . 5 3 10</lang>

Using Pascal's triangle: <lang K> pascal:{x{+':0,x,0}\1}

  pascal 5

(1

1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1)
  {[n;k](pascal n)[n;k]} . 5 3

10</lang>

<lang logo>to choose :n :k

 if :k = 0 [output 1]
 output (choose :n :k-1) * (:n - :k + 1) / :k

end

show choose 5 3  ; 10 show choose 60 30 ; 1.18264581564861e+17</lang>

Lua

<lang lua>function Binomial( n, k )

   if k > n then return nil end
   if k > n/2 then k = n - k end       --   (n k) = (n n-k)
   
   numer, denom = 1, 1
   for i = 1, k do
       numer = numer * ( n - i + 1 )
       denom = denom * i
   end
   return numer / denom

end</lang>

Liberty BASIC

<lang lb>

   '   [RC] Binomial Coefficients
   print "Binomial Coefficient of "; 5; " and "; 3; " is ",BinomialCoefficient( 5, 3)
   n =1 +int( 10 *rnd( 1))
   k =1 +int( n *rnd( 1))
   print "Binomial Coefficient of "; n; " and "; k; " is ",BinomialCoefficient( n, k)
   end
   function BinomialCoefficient( n, k)
       BinomialCoefficient =factorial( n) /factorial( n -k) /factorial( k)
   end function
   function factorial( n)
       if n <2 then
           f =1
       else
           f =n *factorial( n -1)
       end if
   factorial =f
   end function

</lang>

Mathematica

<lang Mathematica>(Local) In[1]:= Binomial[5,3] (Local) Out[1]= 10</lang>

MATLAB

This is a built-in function in MATLAB called "nchoosek(n,k)". But, this will only work for scalar inputs. If "n" is a vector then "nchoosek(v,k)" finds all combinations of choosing "k" elements out of the "v" vector (see Combinations#MATLAB).

Solution: <lang MATLAB>>> nchoosek(5,3)

ans =

   10</lang>

If you want a vectorized function that returns multiple binomial coefficients given vector inputs, you must define that function yourself. A sample implementation is given below. This function takes either scalar or vector inputs for "n" and "v" and returns either a: scalar, vector, or matrix. Where the columns are indexed by the "k" vector and the rows indexed by the "n" vector. binomialCoeff.m: <lang MATLAB>function coefficients = binomialCoeff(n,k)

   coefficients = zeros(numel(n),numel(k)); %Preallocate memory
   columns = (1:numel(k)); %Preallocate row and column counters
   rows = (1:numel(n));
   
   %Iterate over every row and column. The rows represent the n number,
   %and the columns represent the k number. If n is ever greater than k,
   %the nchoosek function will throw an error. So, we test to make sure
   %it isn't, if it is then we leave that entry in the coefficients matrix
   %zero. Which makes sense combinatorically.
   for row = rows
       for col = columns
           if k(col) <= n(row)
               coefficients(row,col) = nchoosek(n(row),k(col));
           end
       end
   end
   

end %binomialCoeff</lang> Sample Usage: <lang MATLAB>>> binomialCoeff((0:5),(0:5))

ans =

    1     0     0     0     0     0
    1     1     0     0     0     0
    1     2     1     0     0     0
    1     3     3     1     0     0
    1     4     6     4     1     0
    1     5    10    10     5     1

>> binomialCoeff([1 0 3 2],(0:3))

ans =

    1     1     0     0
    1     0     0     0
    1     3     3     1
    1     2     1     0

>> binomialCoeff(3,(0:3))

ans =

    1     3     3     1

>> binomialCoeff((0:3),2)

ans =

    0
    0
    1
    3

>> binomialCoeff(5,3)

ans =

   10</lang>

OCaml

<lang OCaml> let binomialCoeff n p =

 let p = if p < n -. p then p else n -. p in
 let rec cm res num denum =
   (* this method partially prevents overflow.
    * float type is choosen to have increased domain on 32-bits computer,
    * however algorithm ensures an integral result as long as it is possible
    *)
   if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.)
   else res in
 cm 1. n 1.

</lang>

Alternate version using big integers

<lang ocaml>#load "nums.cma";; open Num;;

let binomial n p = let m = min p (n - p) in if m < 0 then Int 0 else let rec a j v = if j = m then v else a (j + 1) (quo_num (v */ (num_of_int (n - j))) (num_of_int (j + 1))) in a 0 (Int 1);;</lang>

Simple recursive version

<lang OCaml>open Num;; let rec binomial n k = if n = k then Int 1 else ((binomial (n-1) k) */ Int n) // Int (n-k)</lang>

Oz

Translation of: Python

<lang oz>declare

 fun {BinomialCoeff N K}
    {List.foldL {List.number 1 K 1}
     fun {$ Z I}
        Z * (N-I+1) div I
     end
     1}
 end

in

 {Show {BinomialCoeff 5 3}}</lang>

PARI/GP

<lang parigp>binomial(5,3)</lang>

Pascal

See Delphi

Perl

<lang perl>sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n + 1 - $_, $r /= $_ } $r; }

print binomial(30, 13);</lang>

Perl 6

<lang perl6>multi sub postfix:<!>(Int $a) {

   [*] 1..$a;

}

sub binomialcoefficient($n, $k) {

   $n! / (($n - $k)! * $k!);

}

say binomialcoefficient(5, 3);</lang> Output:

10

This particular piece of code implements the factorial function as a custom operator, which allows you to use it as you would in mathematical equations. The multi keyword on line 1 is to allow operator overloading.

PL/I

<lang PL/I> binomial_coefficients:

  procedure options (main);
     declare (n, k) fixed;
  get (n, k);
  put (coefficient(n, k));

coefficient: procedure (n, k) returns (fixed decimal (15));

  declare (n, k) fixed;
  return (fact(n)/ (fact(n-k) * fact(k)) );

end coefficient;

fact: procedure (n) returns (fixed decimal (15));

  declare n fixed;
  declare i fixed, f fixed decimal (15);
  f = 1;
  do i = 1 to n;
     f = f * i;
  end;
  return (f);

end fact; end binomial_coefficients; </lang> Output: <lang>

               10

</lang>

PHP

<lang PHP><?php $n=5; $k=3; function factorial($val){

   for($f=2;$val-1>1;$f*=$val--);
   return $f;

} $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?></lang>


PicoLisp

<lang PicoLisp>(de binomial (N K)

  (let f '((N) (apply * (range 1 N)))
     (/ (f N) (* (f (- N K)) (f K))) ) )</lang>

Output:

: (binomial 5 3)
-> 10

PureBasic

<lang PureBasic>Procedure Factor(n)

 Protected Result=1
 While n>0
   Result*n
   n-1
 Wend
 ProcedureReturn Result

EndProcedure

Macro C(n,k)

 (Factor(n)/(Factor(k)*factor(n-k)))

EndMacro

If OpenConsole()

 Print("Enter value n: "): n=Val(Input())
 Print("Enter value k: "): k=Val(Input())
 PrintN("C(n,k)= "+str(C(n,k)))
 
 Print("Press ENTER to quit"): Input()
 CloseConsole()

EndIf</lang> Example

Enter value n: 5
Enter value k: 3
C(n,k)= 10

Python

Straight-forward implementation: <lang python>def binomialCoeff(n, k):

   result = 1
   for i in range(1, k+1):
       result = result * (n-i+1) / i
   return result

if __name__ == "__main__":

   print(binomialCoeff(5, 3))</lang>

Output:

10

Alternate implementation <lang python>from operator import mul def comb(n,r):

    calculate nCr - the binomial coefficient
   >>> comb(3,2)
   3
   >>> comb(9,4)
   126
   >>> comb(9,6)
   84
   >>> comb(20,14)
   38760
   
   
   if r > n-r:  # for smaller intermediate values
       r = n-r
   return int( reduce( mul, range((n-r+1), n+1), 1) /
     reduce( mul, range(1,r+1), 1) )</lang>

R

R's built-in choose() function evaluates binomial coefficients: <lang r>choose(5,3)</lang>

Output: <lang r>[1] 10</lang>

REXX

The task is to compute ANY binomial coefficient(s), but this example is limited to 100k digits. <lang rexx>/*REXX program calculates binomial coefficients (aka, combinations). */ numeric digits 100000 parse arg n k . say 'comb('n","k')='comb(n,k) exit

comb: procedure; parse arg x,y; return fact(x)/(fact(x-y)*fact(y)) fact: procedure; parse arg z; !=1; do j=2 to z; !=!*j; end; return !</lang> Output when using the input of:

5 3
comb(5,3)=10

Output when using the input of:

1200 120
comb(1200,120)=1004576581793084916475353119318331966507299414258370667602185866686463289093457468590558508056798211449853806741873396451735444387513582540860551330127062642417424083600

Ruby

Translation of: Tcl
Works with: Ruby version 1.8.7+

<lang ruby>class Integer

 # binomial coefficient: n C k
 def choose(k)
   # n!/(n-k)!
   pTop = (self-k+1 .. self).inject(1, &:*) 
   # k!
   pBottom = (2 .. k).inject(1, &:*)
   pTop / pBottom
 end

end

p 5.choose(3) p 60.choose(30)</lang> result

10
118264581564861424

another implementation:

<lang ruby> def c n, r

 (0...r).inject(1) do |m,i| (m * (n - i)) / (i + 1) end

end </lang>

Scala

<lang scala>object Binomial {

  def main(args: Array[String]): Unit = {
     val n=5
     val k=3
     val result=binomialCoefficient(n,k)
     println("The Binomial Coefficient of %d and %d equals %d.".format(n, k, result))
  }
  def binomialCoefficient(n:Int, k:Int)=fact(n) / (fact(k) * fact(n-k))
  def fact(n:Int):Int=if (n==0) 1 else n*fact(n-1)

}</lang> Output: <lang>The Binomial Coefficient of 5 and 3 equals 10.</lang>

Scheme

Works with: Scheme version RRS

<lang scheme>(define (factorial n)

 (define (*factorial n acc)
   (if (zero? n)
       acc
       (*factorial (- n 1) (* acc n))))
 (*factorial n 1))

(define (choose n k)

 (/ (factorial n) (* (factorial k) (factorial (- n k)))))

(display (choose 5 3)) (newline)</lang> Output: <lang>10</lang>

Seed7

<lang seed7>$ include "seed7_05.s7i";

const func integer: binomial (in integer: n, in var integer: k) is func

 result
   var integer: binomial is 0;
 local
   var integer: l is 0;
 begin
   if n >= k then
     if k > n - k then
       k := n - k;  # Optimization
     end if;
     binomial := 1;
     l := 0;
     while l < k do
       binomial *:= n - l;
       incr(l);
       binomial := binomial div l;
     end while;
   end if;
 end func;

const proc: main is func

 begin
   writeln("binomial coefficient of (5, 3) is " <& binomial(5, 3));
 end func;</lang>

Output:

binomial coefficient of (5, 3) is 10

Tcl

This uses exact arbitrary precision integer arithmetic. <lang tcl>package require Tcl 8.5 proc binom {n k} {

   # Compute the top half of the division; this is n!/(n-k)!
   set pTop 1
   for {set i $n} {$i > $n - $k} {incr i -1} {

set pTop [expr {$pTop * $i}]

   }
   # Compute the bottom half of the division; this is k!
   set pBottom 1
   for {set i $k} {$i > 1} {incr i -1} {

set pBottom [expr {$pBottom * $i}]

   }
   # Integer arithmetic divide is correct here; the factors always cancel out
   return [expr {$pTop / $pBottom}]

}</lang> Demonstrating: <lang tcl>puts "5_C_3 = [binom 5 3]" puts "60_C_30 = [binom 60 30]"</lang> Output:

5_C_3 = 10
60_C_30 = 118264581564861424

TI-89 BASIC

Builtin function.

<lang ti89b>nCr(n,k)</lang>

UNIX Shell

<lang sh>#!/bin/sh n=5; k=3; calculate_factorial(){ partial_factorial=1; for (( i=1; i<="$1"; i++ )) do

   factorial=$(expr $i \* $partial_factorial)
   partial_factorial=$factorial

done echo $factorial }

n_factorial=$(calculate_factorial $n) k_factorial=$(calculate_factorial $k) n_minus_k_factorial=$(calculate_factorial `expr $n - $k`) binomial_coefficient=$(expr $n_factorial \/ $k_factorial \* 1 \/ $n_minus_k_factorial )

echo "Binomial Coefficient ($n,$k) = $binomial_coefficient"</lang>


Ursala

A function for computing binomial coefficients (choose) is included in a standard library, but if it weren't, it could be defined in one of the following ways, starting from the most idiomatic. <lang Ursala>#import nat

choose = ~&ar^?\1! quotient^\~&ar product^/~&al ^|R/~& predecessor~~</lang> The standard library functions quotient, product and predecessor pertain to natural numbers in the obvious way.

  • choose is defined using the recursive conditional combinator (^?) as a function taking a pair of numbers, with the predicate ~&ar testing whether the number on the right side of the pair is non-zero.
  • If the predicate does not hold (implying the right side is zero), then a constant value of 1 is returned.
  • If the predicate holds, the function given by the rest of the expression executes as follows.
  • First the predecessor of both sides (~~) of the argument is taken.
  • Then a recursive call (^|R) is made to the whole function (~&) with the pair of predecessors passed to it as an argument.
  • The result returned by the recursive call is multiplied (product) by the left side of the original argument (~&al).
  • The product of these values is then divided (quotient) by the right side (~&ar) of the original argument and returned as the result.

Here is a less efficient implementation more closely following the formula above. <lang Ursala>choose = quotient^/factorial@l product+ factorial^~/difference ~&r</lang>

  • choose is defined as the quotient of the results of a pair (^) of functions.
  • The left function contributing to the quotient is the factorial of the left side (@l) of the argument, which is assumed to be a pair of natural numbers. The factorial function is provided in a standard library.
  • The right function contributing to the quotient is the function given by the rest of the expression, which applies to the whole pair as follows.
  • It begins by forming a pair of numbers from the argument, the left being their difference obtained by subtraction, and the right being the a copy of the right (~&r) side of the argument.
  • The factorial function is applied separately to both results (^~).
  • A composition (+) of this function with the product function effects the multiplication of the two factorials, to complete the other input to the quotient.

Here is an equivalent implementation using pattern matching, dummy variables, and only the apply-to-both (~~) operator. <lang Ursala>choose("n","k") = quotient(factorial "n",product factorial~~ (difference("n","k"),"k"))</lang> test program: <lang Ursala>#cast %nL

main = choose* <(5,3),(60,30)></lang> output:

<10,118264581564861424>