MathMathematical Algorithms

Binomial Coefficients (Pascal's Triangle)

Count arrangements and selections: nCr via factorial and inverse-factorial tables mod p, Pascal's triangle, stars and bars, and inclusion-exclusion.

Learn Combinatorics →
k=0k=1k=2k=3k=4k=5k=6k=7k=8
n=01········
n=1·········
n=2·········
n=3·········
n=4·········
n=5·········
n=6·········
n=7·········
n=8·········
row sums (every subset of an n-element set)
nrowsum2^n
0111
1/70C(n, k) counts the k-element subsets of an n-element set. Start from the only thing that is obvious: there is exactly one subset of the empty set, so C(0, 0) = 1. Everything else in the triangle follows from a single question asked about one element.
Cell being filledThe two cells it is the sum ofEdge of the triangle (always 1)ComputedValue being read off / identity being shown
1C[0][0] = 1
2for n in 1 .. N:
3 C[n][0] = C[n][n] = 1 # one way to choose none, one way to choose all
4 for k in 1 .. n-1:
5 C[n][k] = C[n-1][k-1] + C[n-1][k] # element n is either used or it is not
6read C(N, K); row n sums to 2^n; C(n, k) == C(n, n-k)
Variables
N8
K3
Complexity
best O(1)
avg O(1)
worst O(n^2)
space O(n)
Speed