On this page
Draw the pairs of natural numbers as dots on a grid. Every row and column is infinite, so it feels as if there must be "more" pairs than single numbers. The Cantor pairing function shows there are not: every pair gets its own natural number, and every natural number belongs to exactly one pair.
What is the Cantor pairing function?
The Cantor pairing function is π(x, y) = (x + y)(x + y + 1)/2 + y, defined for x, y in {0, 1, 2, 3, ...} (Wikipedia: Pairing function).
It is a bijection from ℕ × ℕ to ℕ: one-to-one and onto.
The idea behind it is a counting route. Group the pairs by their sum w = x + y. The diagonal w holds w + 1 pairs. Walk the diagonals in order, and along each one start at (w, 0) and climb to (0, w).
Before you reach diagonal w you have already counted 1 + 2 + ... + w = w(w + 1)/2 pairs, a triangular number. Inside the diagonal, y tells you how many steps you have climbed. So the label of (x, y) is the triangular number for its diagonal plus y.
How do you calculate the Cantor pairing function?
Use the two-step recipe: find w = x + y, then add y to w(w + 1)/2.
π(0, 0): w = 0, so 0 + 0 = 0.
π(2, 1): w = 3, so 6 + 1 = 7.
π(3, 4): w = 7, so 28 + 4 = 32.
π(4, 9): w = 13, so 91 + 9 = 100.
π(52, 1): w = 53, so 1431 + 1 = 1432, the value worked on the Wikipedia page.
Order matters: π(1, 2) = 6 + 2 = 8, while π(2, 1) = 7. The function is not symmetric, and that is what keeps (1, 2) and (2, 1) apart.
How do you invert the Cantor pairing function?
Given a number z, you first find its diagonal, then its place on it. The inverse is w = ⌊(√(8z + 1) − 1)/2⌋, t = w(w + 1)/2, y = z − t and x = w − y (Wikipedia: Pairing function).
The first step finds the largest triangular number not above z: solve w(w + 1)/2 = z with the quadratic formula and round down.
Try z = 100:
√801 is about 28.3, so w = ⌊27.3/2⌋ = 13.
t = 13 × 14/2 = 91.
y = 100 − 91 = 9.
x = 13 − 9 = 4.
So 100 decodes to (4, 9), matching the worked value above. If y comes out negative or bigger than w, recheck your w.
Why does it show N × N is countable?
A set is countable when you can list it as first, second, third and so on, which is the same as a bijection with ℕ. The pairing function is exactly such a list for ℕ × ℕ, so the grid of pairs has the same size as ℕ. If you want the general tool behind this, our post on counting with a bijection walks through three matchings.
The same trick carries further. Wikipedia notes that any pairing function can be used to prove that the integers and the rational numbers have the same cardinality as the natural numbers (Wikipedia: Pairing function); MathWorld credits the result for the rationals to Georg Cantor (MathWorld: Pairing Function). Each fraction p/q is fixed by a pair of whole numbers, so the fractions are no bigger than the grid; the Cantor–Schröder–Bernstein theorem turns "no bigger than" into an exact match. Not every infinite set can be listed: the Cantor set has uncountably many points.
The Fueter–Pólya theorem says Cantor's polynomial and its mirror image π(y, x) are the only quadratic polynomial pairing functions; whether any other polynomial works is still an open question (Wikipedia: Fueter–Pólya theorem).
How do you extend it to triples?
Pair twice. MathWorld notes that ⟨i, j, k⟩ can be defined as ⟨i, ⟨j, k⟩⟩ or ⟨⟨i, j⟩, k⟩, and suggests ⟨⟨i, j⟩, ⟨k, l⟩⟩ for four numbers to keep the result smaller (MathWorld: Pairing Function).
Worked triple, using π(π(x, y), z) for (1, 2, 3):
π(1, 2) = 8.
π(8, 3): w = 11, so 66 + 3 = 69.
To decode 69, invert once to get (8, 3), then invert 8 to get (1, 2). Repeating this shows that ℕ × ℕ × ℕ, and any finite power of ℕ, is countable.
If triangular numbers or bijections were new to you, the Fermi Concept Library gives every concept in mathematics its own lesson. To try more encoding and counting problems, use the Fermi Problem Library, whose step-by-step solver explains the reasoning.



