Evaluate the Binomial Coefficient

8

0

Given two nonnegative integers n,k such that 0 <= k <= n, return the binomial coefficient

c(n,k) := (n!) / (k! * (n-k)!)

Test cases

Most languages will probably have a built in function.

c(n,0) = c(n,n) = 1 for all n
c(n,1) = c(n,n-1) = n for all n 
c(5,3) = 10
c(13,5) = 1287

Related challenges

Catalan Numbers Compute the multinomial coefficient Generate Pascal's triangle m-nomial coefficient

flawr

Posted 2016-12-10T23:26:47.393

Reputation: 40 560

Question was closed 2016-12-11T04:25:16.813

4

Surely this is a dup http://codegolf.stackexchange.com/questions/1744/mathematical-combination/9150#9150 or am I missing something?

– Digital Trauma – 2016-12-11T03:31:21.473

1I was not aware of that, and I did a thorough search / had it in the sandbox for quite a long time. Too bad it doesn't contain the keyword binomial coefficient... – flawr – 2016-12-11T10:02:09.083

Answers

5

JavaScript (ES6), 27 bytes

f=(n,k)=>k?n*f(n-1,k-1)/k:1

Arnauld

Posted 2016-12-10T23:26:47.393

Reputation: 111 334

5

MATL, 2 bytes

Xn

try it online!

flawr

Posted 2016-12-10T23:26:47.393

Reputation: 40 560

3

Dennis

Posted 2016-12-10T23:26:47.393

Reputation: 196 637

3

CJam, 11 bytes

l~S*\0e]e!,

Try it online!

Explanation

This uses a trick that (I think) I first used for the Catalan numbers challenge. CJam doesn't have a built-in for this, and computing three factorials is too expensive. But the binomial coefficient c(n,k) is the number of ways we can select k out of n elements. That is, it's equal to the number of permutations of a list of n elements where k of them have one value and the remaining have another.

l~   e# Read and evaluate input. Dumping n and k on the stack.
S*   e# Get a string of k spaces.
\0e] e# Pad to length n with zeros.
e!   e# Get the unique permutations.
,    e# Count the number of such permutations.

Martin Ender

Posted 2016-12-10T23:26:47.393

Reputation: 184 808

2

Matlab, 8 bytes

There is a builtin for this calculation:

nchoosek

flawr

Posted 2016-12-10T23:26:47.393

Reputation: 40 560

2

Haskell, 37 35 bytes

n#0=1
0#k=1
n#k=(n-1)#(k-1)*n`div`k

flawr

Posted 2016-12-10T23:26:47.393

Reputation: 40 560

2

Mathematica, 8 bytes

Binomial

Yup. Sample usage: Binomial[13,5] or 13~Binomial~5 to obtain 1287.

Greg Martin

Posted 2016-12-10T23:26:47.393

Reputation: 13 940

2

Haskell, 34 bytes

n#k|k<1||k>=n=1|m<-n-1=m#(k-1)+m#k

Usage example: 13#5 -> 1287.

A variant with the same size for the k<1||k>=ntest is n*k-k*k<1.

nimi

Posted 2016-12-10T23:26:47.393

Reputation: 34 639

​Very​ ​clever!​​​​ – flawr – 2016-12-11T10:01:00.997

1

Jellyfish, 6 bytes

pCi
 i

Try it online!

C is the built-in for binomial coefficients, the is are replaced with one input each, p prints the result.

Martin Ender

Posted 2016-12-10T23:26:47.393

Reputation: 184 808

1

Python 2, 33 bytes

f=lambda n,k:k<1or n*f(n-1,k-1)/k

Note that this will return True if k = 0, which seems to be allowed by default.

Try it online!

Dennis

Posted 2016-12-10T23:26:47.393

Reputation: 196 637

1

Pyth, 3 bytes

.cF

A program that takes input in the form n,k and prints the result.

Test

How it works

This simply folds c(n,k) over the input.

TheBikingViking

Posted 2016-12-10T23:26:47.393

Reputation: 3 674

1

J, 1 byte

!

Usage:

   3!5
10
   5!13
1287

alephalpha

Posted 2016-12-10T23:26:47.393

Reputation: 23 988

1

Actually, 1 byte

Input is of the form k<newline>n. Try it online!

Sherlock9

Posted 2016-12-10T23:26:47.393

Reputation: 11 664

0

Perl 6, 23 bytes

{+combinations $^n,$^p}
{[*] ($^n...0)Z/1..$^p}

( Both are were based on code found at examples.perl6.org, and RosettaCode )

Brad Gilbert b2gills

Posted 2016-12-10T23:26:47.393

Reputation: 12 713

0

MATL, 10 bytes

:i:&G-:h/p

Try it online!

This does the computation manually, without the builtin:

:     % Take input n implicitly. Range
      % STACK: [1 2 ... n]
i:    % Take input k. Range
      % STACK: [1 2 ... n], [1 2 ... k]
&G-   % Push n and k again. Subtract
      % STACK: [1 2 ... n], [1 2 ... k], n-k
:     % Range
      % STACK: [1 2 ... n], [1 2 ... k], [1 2 ... n-k]
h     % Concatenate the top two arrays horizontally
      % STACK: [1 2 ... n], [1 2 ... k 1 2 ... n-k]
/     % Element-wise division
      % STACK: [1/1 2/2 ... k/k (k+1)/1 (k+2)2 ... n/(n-k)]
p     % Product of array. Implicitly display
      % STACK: 1/1 * 2/2 * ... * k/k * (k+1)/1 * (k+2)2 * ... * n/(n-k)

Luis Mendo

Posted 2016-12-10T23:26:47.393

Reputation: 87 464