nchoosek

Binomial coefficient: the number of ways to choose k of n items.

Category: Mathematics > Elementary Math

Syntax

C = nchoosek(n, k)

Returns the binomial coefficient n!/(k!*(n-k)!).

Description

Order does not matter and no item is repeated, so nchoosek(5, 2) counts the 10 pairs from five items. The values form Pascal's triangle: nchoosek(n, k) equals nchoosek(n-1, k-1) + nchoosek(n-1, k). The result is exact only while it stays below 2^53; for larger values the number is rounded and MATLAB warns about it.

In MATLAB, passing a vector as the first argument lists all combinations of its elements as rows of a matrix.

Input arguments

n
Nonnegative integer scalar, the number of items.
k
Integer scalar from 0 to n, the size of each selection.

Output arguments

C
Scalar count of the unordered selections.

Example

Counting selections

nchoosek(5, 2)
nchoosek(30, 15)
nchoosek(5, 0)
Output
ans = 10
ans = 155117520
ans = 1

Notes

Cell array input to nchoosek is not supported in MatLivre.

See also

factorial perms gamma gammaln primes