Cantor Pairing Function Calculator
Pairing
- The Cantor pairing function pi(a, b) = (a + b)(a + b + 1) / 2 + b maps a pair of whole numbers to a single whole number.
- Here a + b = 8, so the triangular number is 8 x 9 / 2 = 36.
- Adding b = 5 gives pi(3, 5) = 36 + 5 = 41.
- The pairing is a bijection: every pair gets its own number and every whole number decodes back to exactly one pair, which is how Cantor showed the rational numbers are countable.
The Cantor pairing function squeezes two whole numbers into a single whole number, losslessly. Give it a pair such as (3, 5) and it returns one number, 41. Give it 41 and it hands back exactly (3, 5). No information is created or destroyed: the mapping is a bijection, a perfect one-to-one correspondence between pairs of non-negative integers and single non-negative integers.
Georg Cantor introduced the idea in 1878 while proving that the rational numbers are countable. If pairs of whole numbers can be enumerated by single whole numbers, then fractions - which are pairs of integers - can be enumerated too, and a set that can be enumerated is exactly as large as the counting numbers themselves. The pairing function is the concrete machine behind that famous argument.
This calculator works in both directions. Pairing mode applies the formula pi(a, b) = (a + b)(a + b + 1) / 2 + b. Unpairing mode inverts it: it finds the diagonal the number lives on and reads the pair back off. Every step is shown, so you can follow the arithmetic rather than trusting a black box.
Worked examples
Pairing 3 and 5
(0 - pair (a, b) into one number · 3 · 5 · 41)
| Cantor pairing value pi(3, 5) | 41 |
| diagonal a + b | 8 |
| triangular number T(a + b) | 36 |
| offset of b along the diagonal | 5 |
Pairing 10 and 20
(0 - pair (a, b) into one number · 10 · 20 · 485)
| Cantor pairing value pi(10, 20) | 485 |
| diagonal a + b | 30 |
| triangular number T(a + b) | 465 |
| offset of b along the diagonal | 20 |
Unpairing 41
(1 - unpair a number back into (a, b) · 3 · 5 · 41)
| pair hidden in 41 | a = 3, b = 5 |
| diagonal w | 8 |
| triangular number T(w) | 36 |
| check: pairing the answer again | 41 |
What the Cantor pairing function is
Picture the grid of all pairs (a, b) laid out with a across and b down. Cantor's enumeration does not run along rows or columns, which would never finish the first one. Instead it sweeps along diagonals, the lines where a + b is constant. The first diagonal, a + b = 0, holds just (0, 0). The next holds (1, 0) and (0, 1). The next holds (2, 0), (1, 1) and (0, 2), and so on, each diagonal one pair longer than the last.
Counting along the diagonals in order assigns every pair a position. The pairs before diagonal s number 1 + 2 + ... + s = s(s + 1) / 2, the triangular number T(s). Within diagonal s, the pair (a, b) sits at offset b, because the enumeration runs from b = 0 up to b = s. Adding the two pieces gives the formula: pi(a, b) = T(a + b) + b = (a + b)(a + b + 1) / 2 + b.
- Diagonal: all pairs with the same sum s = a + b
- Triangular number T(s) = s(s + 1) / 2 counts the pairs on earlier diagonals
- Offset within the diagonal is simply b
- pi(0, 0) = 0, pi(1, 0) = 1, pi(0, 1) = 2, pi(2, 0) = 3
How to pair two numbers
Take the pair (3, 5). Their sum is 8, so they live on diagonal 8. The triangular number T(8) = 8 x 9 / 2 = 36 counts the pairs on diagonals 0 through 7. Adding the offset b = 5 gives pi(3, 5) = 36 + 5 = 41. That is the whole computation: one addition, one triangular number, one more addition.
A larger pair works identically. For (10, 20) the sum is 30, T(30) = 30 x 31 / 2 = 465, and the offset is 20, so pi(10, 20) = 485. Notice how the size of the answer is governed mostly by the sum a + b, not by either number alone: the pairing treats the pair as a point on a diagonal first and a position along it second.
The calculator reports all three pieces - the diagonal, the triangular number and the offset - so a result can always be checked by hand in seconds.
- pi(3, 5): sum 8, T(8) = 36, offset 5, answer 41
- pi(10, 20): sum 30, T(30) = 465, offset 20, answer 485
- Swapping the inputs changes the answer: pi(5, 3) = 39, not 41
- Order matters because the offset counts b, not a
How to unpair a number
Unpairing runs the geometry backwards. Given z, find the diagonal w whose triangular number is the largest one not exceeding z: T(w) <= z < T(w + 1). Solving the quadratic gives w = floor((sqrt(8z + 1) - 1) / 2). Then b = z - T(w) is the offset along the diagonal, and a = w - b completes the pair.
For z = 41: 8z + 1 = 329, whose square root is about 18.1, so w = floor((18 - 1) / 2) = 8. Then T(8) = 36, b = 41 - 36 = 5 and a = 8 - 5 = 3. The recovered pair is (3, 5), and pairing it again returns 41, which is the round-trip check the calculator shows.
The unpairing is exact integer arithmetic - the square root only locates the diagonal, and the triangular-number subtraction does the rest - so there is no rounding ambiguity even for very large z.
- Find w with T(w) <= z < T(w + 1)
- b = z - T(w), then a = w - b
- z = 41 gives w = 8, b = 5, a = 3
- Pairing the recovered pair always reproduces z
Why Cantor invented it
In the 1870s Cantor was building set theory and asking a question that sounded absurd at the time: are all infinite sets the same size? The pairing function gave him a decisive tool. Because pairs of whole numbers can be listed by single whole numbers, the set of pairs is countable. A rational number is a pair (numerator, denominator), so the rationals are countable too - no larger than the whole numbers, despite lying densely between them.
The same trick extends. Triples can be handled by pairing twice: pi(pi(a, b), c). Any fixed-length tuple of whole numbers collapses to one whole number, and finite sequences can be encoded with a length prefix. Modern descendants of the idea appear wherever two coordinates must become one key: database composite keys, hash functions for grid coordinates, and space-filling enumerations in computability theory.
It is worth noting what the pairing function is not. It is not a compression scheme - the output is typically larger than either input, growing like (a + b)^2 / 2. Its value is exactness and reversibility, not smallness.
- Proves pairs of whole numbers are countable
- Hence the rational numbers are countable
- Tuples: pair repeatedly, pi(pi(a, b), c)
- Modern uses: composite keys, grid hashing, enumerations
Properties worth knowing
The Cantor pairing function is a polynomial - in fact the only quadratic polynomial that bijects the non-negative integer grid with the non-negative integers in this diagonal fashion, a result known as the Fueter-Polya theorem. That uniqueness makes it the canonical choice rather than one option among many.
Its growth is quadratic along diagonals: pi(a, b) is roughly (a + b)^2 / 2. Along the axes it reduces to triangular numbers: pi(a, 0) = T(a) and pi(0, b) = T(b) + b = T(b + 1) - 1 + ... more precisely pi(0, b) = b(b + 1) / 2 + b = b(b + 3) / 2. The inverse grows like sqrt(2z), which is why unpairing starts with a square root.
There is a mirror variant that offsets by a instead of b, enumerating each diagonal in the opposite direction. The two are related by swapping the inputs, and different textbooks adopt different conventions - if a reference gives pi(3, 5) = 39, it is using the mirror form, and its unpairing will still be consistent within itself.
- The unique quadratic pairing polynomial (Fueter-Polya)
- Grows like (a + b)^2 / 2; the inverse grows like sqrt(2z)
- pi(a, 0) = T(a), the triangular numbers
- A mirror convention offsets by a instead of b
Common mistakes and checks
The most common error is swapping the roles of the inputs. Because the offset counts b, pi(a, b) and pi(b, a) differ whenever a and b do: pi(3, 5) = 41 but pi(5, 3) = 39. Both are correct outputs of the same function - the pair is ordered. If a reference or a colleague gets the other value, check whether their convention offsets by a instead; the mirror convention enumerates each diagonal in the opposite direction and is self-consistent, just different.
A second check is magnitude. The answer should be close to (a + b)^2 / 2, within the width of one diagonal. If a computed pairing is wildly larger or smaller than that, an arithmetic slip has occurred - usually a forgotten division by 2 in the triangular number, which doubles the result. The calculator's decomposition into diagonal, triangular number and offset makes such slips easy to localise: verify T(s) first, then the offset.
Finally, unpairing offers the strongest verification of all: pair the recovered pair and demand the original number back. The calculator performs this check internally for its unpairing mode and shows it as the third secondary value. Any implementation of the Cantor pairing, in any language, should be tested exactly this way - round-trip on a spread of values including the diagonal boundaries z = T(w) and z = T(w + 1) - 1, where off-by-one errors like to hide.
- pi(a, b) is not pi(b, a): the pair is ordered
- Sanity check: the answer is near (a + b)^2 / 2
- Round-trip test: unpair, then pair, and compare
- Boundary cases T(w) and T(w + 1) - 1 catch off-by-one errors
FAQs
What is the Cantor pairing function?
It is the bijection pi(a, b) = (a + b)(a + b + 1) / 2 + b between pairs of whole numbers and single whole numbers. For example pi(3, 5) = 41.
What is pi(10, 20)?
485. The sum is 30, the triangular number T(30) = 465 counts the earlier pairs, and the offset b = 20 gives 465 + 20 = 485.
How do I unpair the number 41?
Find the largest triangular number not exceeding 41, which is T(8) = 36. Then b = 41 - 36 = 5 and a = 8 - 5 = 3, so 41 decodes to (3, 5).
Is the Cantor pairing function reversible?
Yes, exactly. It is a bijection, so every whole number decodes back to precisely one pair, and pairing that pair reproduces the number.
Which diagonal does the pair (3, 5) sit on?
Diagonal 8, since a + b = 8. That diagonal starts at the triangular number T(8) = 36, and (3, 5) is the pair at offset 5 along it.
Why is the pairing function important?
Cantor used it to show that pairs of whole numbers are countable, which is the key step in proving the rational numbers are countable - the same size as the whole numbers.