Lesson 9 of 15
Advanced Combinatorics
Stars and Bars, Stirling Numbers, Catalan Numbers
Stars and Bars
The number of ways to distribute identical objects into distinct bins (allowing empty bins) is:
Intuition: arrange stars and bars in a row — each arrangement defines a distribution.
Example: 3 coins into 2 pockets: ways (0+3, 1+2, 2+1, 3+0... but 4 because 0 included).
Wait: 5 balls into 3 bins: .
Stirling Numbers of the Second Kind
counts the ways to partition labeled elements into non-empty unlabeled subsets:
Catalan Numbers
They count: valid parenthesizations, full binary trees, monotone lattice paths, and more. .
import math
def stars_and_bars(n, k):
return math.comb(n + k - 1, k - 1)
def catalan(n):
return math.comb(2 * n, n) // (n + 1)
print(stars_and_bars(5, 3)) # 21
print(catalan(5)) # 42
Your Task
Implement stars_and_bars(n, k), stirling_second(n, k), and catalan(n).
Pyodide loading...
Loading...
Click "Run" to execute your code.