# nchoosek

Binomial coefficient

nchoosek(n,k)

## Description

nchoosek(n,k) returns the binomial coefficient of n and k.

## Input Arguments

 n Symbolic number, variable or expression. k Symbolic number, variable or expression.

## Examples

Compute the binomial coefficients for these expressions:

syms n
[nchoosek(n, n), nchoosek(n, n + 1), nchoosek(n, n - 1)]
ans =
[ 1, 0, n]

If one or both parameters are negative numbers, convert these numbers to symbolic objects:

[nchoosek(sym(-1), 3), nchoosek(sym(-7), 2), nchoosek(sym(-5), -5)]
ans =
[ -1, 28, 1]

If one or both parameters are complex numbers, convert these numbers to symbolic objects:

[nchoosek(sym(i), 3), nchoosek(sym(i), i), nchoosek(sym(i), i + 1)]
ans =
[ 1/2 + 1i/6, 1, 0]

Differentiate the binomial coefficient:

syms n
diff(nchoosek(n, 2))
ans =
-(psi(n - 1) - psi(n + 1))*nchoosek(n, 2)

Expand the binomial coefficient:

syms n k
expand(nchoosek(n, k))
ans =
-(n*gamma(n))/(k^2*gamma(k)*gamma(n - k) - k*n*gamma(k)*gamma(n - k))

expand all

### Binomial Coefficient

If n and k are integers and 0 ≤ k ≤ n, the binomial coefficient is defined as:

$\left(\begin{array}{c}n\\ k\end{array}\right)=\frac{n!}{k!\left(n-k\right)!}$

For complex numbers, the binomial coefficient is defined via the gamma function:

$\left(\begin{array}{c}n\\ k\end{array}\right)=\frac{\Gamma \left(n+1\right)}{\Gamma \left(k+1\right)\Gamma \left(n-k+1\right)}$

### Tips

• Calling nchoosek for numbers that are not symbolic objects invokes the MATLAB® nchoosek function.

• If one or both parameters are complex or negative numbers, convert these numbers to symbolic objects using sym, and then call nchoosek for those symbolic objects.

### Algorithms

If k < 0 or n – k < 0, nchoosek(n,k) returns 0.

If one or both arguments are complex, nchoosek uses the formula representing the binomial coefficient via the gamma function.