Skip to the workspace
Pebellslearn by building
Number Theory for Algorithms  ›  Lesson

Counting Without Listing

Combinatorics in Code 24 minPascal's triangle is the first DP table; the factorial-and-modular-inverse route answers C(n, r) mod p for huge n

dsa / Counting Without Listing

Explore each scene at your own pace.

01 · Splitting a bill among friends, the counting version

Splitting a bill among friends, the counting version

Counting Without Listing

Definition
C(n, r), read as n choose r, is the number of ways to pick r items from n when order does not matter. It can be built from Pascal's triangle, where each cell is the sum of the two above it, or from factorials with a modular inverse.
In plain words
It answers how many different teams of 3 you can pick from 8 friends, without writing the teams out. Pascal's triangle gets there by adding neighbours row by row; the factorial formula gets there in one shot.

In short: C(n, r), "n choose r", counts how many r-item subsets an n-item set has, without ever listing them. Two ways to compute it: Pascal's triangle, where every cell is the sum of the two cells above it — the first dynamic-programming table most people meet, often years before they'd call it that — and the factorial formula n! / (r! · (n-r)!), which needs a modular inverse to divide under a modulus, since there's no / in modular arithmetic. Quarry's hash kernel uses both: Pascal's triangle to explain where the numbers come from, and the factorial-and-inverse route to compute C(n, r) mod p for huge n without building a triangle that big.

Pascal's triangle sketched by hand, with two curved arrows from a row into the cell below that sums them

Splitting a bill among friends, the counting version

Eight friends split into a table of 3 and a table of 5 for a trip's two cars. How many different groups of 3 could go in the first car? That's C(8, 3) — order inside the car doesn't matter, only who's in it.

python

This works for small n, but num is a product of r numbers close to n — for n in the millions, num becomes an enormous integer before the division ever happens. That's fine in Python's big-int arithmetic, but slow, and it's exactly the kind of growth Quarry's hash kernel needs to avoid when counting collisions over billions of keys.

Scene 1 of 8
Project context and further resources
You're building a piece ofQuarry — the hash kernel
This piece — ncr_mod(): Counts arrangements and collisions mod p, for n too large to list anything.
Scenario Quarry's hash kernel reports how many ways a collision of a given size could have happened by chance, to decide whether a cluster of matching hashes is suspicious — and n can run into the billions.
ncr_mod.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
ncr_mod(n, r, mod) → intC(n, r) mod p.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.