← back to discrete math

Counting

Oscar Levin · CC BY-SA 4.0 · Discrete Mathematics: An Open Introduction, Ch. 1

Counting is not about listing things one by one. It is about finding the size of a set without enumeration, using structure: addition when choices are disjoint, multiplication when choices are independent, and correction terms when they overlap.

1 2 3 4 5 C(5,3) = 10 ways to choose the green subset

Additive and multiplicative principles

If you can do task A in m ways or task B in n ways, and the two sets of outcomes are disjoint, you have m + n total options. If you must do A then B, and the choices are independent, you have m * n options. These two rules generate the rest of combinatorics.

Scheme

Binomial coefficients

C(n,k) = n! / (k!(n-k)!) counts the number of k-element subsets of an n-element set. It answers "how many ways can I choose k items from n when order doesn't matter?" The same numbers appear as coefficients in (x+y)^n, which is why they are called binomial coefficients.

Scheme

Permutations vs combinations

A permutation is an ordered arrangement: P(n,k) = n!/(n-k)!. A combination ignores order: C(n,k) = P(n,k)/k!. The ratio k! captures exactly the redundant orderings within each chosen subset.

Scheme

Stars and bars

How many ways can you distribute n identical objects into k distinct bins? Place n stars in a row and insert k-1 bars to create k groups. The answer is C(n+k-1, k-1). This reframes a distribution problem as a subset-selection problem.

Scheme

Principle of inclusion-exclusion

When sets overlap, naive addition overcounts. PIE corrects: |A union B| = |A| + |B| - |A intersect B|. For three sets, add back the triple intersection. The pattern alternates signs. This is the counting world's version of the Mobius function.

Scheme

Notation reference

Math Scheme Python Meaning
C(n,k)(choose n k)comb(n, k)Binomial coefficient
P(n,k)(perm n k)perm(n, k)k-permutation of n
n!(fact n)factorial(n)Factorial
|A union B|(- (+ a b) ab)a + b - abInclusion-exclusion (2 sets)
C(n+k-1, k-1)(stars-and-bars n k)comb(n+k-1, k-1)Stars and bars
Neighbors

Other chapter pages

Foundations (Wikipedia)

Translation notes

Levin's textbook builds counting from careful set-theoretic arguments. The Scheme implementations here use exact integer arithmetic, so no overflow or rounding issues for moderate inputs. The multiplicative formula for C(n,k) avoids computing full factorials. Python's math.comb (3.8+) does the same internally.