In
mathematics
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, a pairing function is a process to uniquely encode two
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
s into a single natural number.
Any pairing function can be used in
set theory
Set theory is the branch of mathematical logic that studies Set (mathematics), sets, which can be informally described as collections of objects. Although objects of any kind can be collected into a set, set theory – as a branch of mathema ...
to prove that
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s and
rational number
In mathematics, a rational number is a number that can be expressed as the quotient or fraction of two integers, a numerator and a non-zero denominator . For example, is a rational number, as is every integer (for example,
The set of all ...
s have the same
cardinality
The thumb is the first digit of the hand, next to the index finger. When a person is standing in the medical anatomical position (where the palm is facing to the front), the thumb is the outermost digit. The Medical Latin English noun for thum ...
as natural numbers.
Definition
A pairing function is a
bijection
In mathematics, a bijection, bijective function, or one-to-one correspondence is a function between two sets such that each element of the second set (the codomain) is the image of exactly one element of the first set (the domain). Equival ...
:
Generalization
More generally, a pairing function on a set
is a function that maps each pair of elements from
into an element of
, such that any two pairs of elements of
are associated with different elements of
, or a bijection from
to
.
Instead of abstracting from the domain, the arity of the pairing function can also be generalized: there exists an n-ary generalized Cantor pairing function on
.
Cantor pairing function
The Cantor pairing function is a
primitive recursive pairing function
:
defined by
:
where
.
It can also be expressed as
.
It is also strictly monotonic w.r.t. each argument, that is, for all
, if
, then
; similarly, if
, then
.
The statement that this is the only quadratic pairing function is known as the
Fueter–Pólya theorem
The Fueter–Pólya theorem, first proved by Rudolf Fueter and George Pólya, states that the only quadratic polynomial pairing functions are the Cantor polynomials.
Introduction
In 1873, Georg Cantor showed that the so-called Cantor polyno ...
. Whether this is the only polynomial pairing function is still an open question. When we apply the pairing function to and we often denote the resulting number as .
This definition can be inductively generalized to the
:
for
as
:
with the base case defined above for a pair:
Inverting the Cantor pairing function
Let
be an arbitrary natural number. We will show that there exist unique values
such that
:
and hence that the function is invertible. It is helpful to define some intermediate values in the calculation:
:
:
:
where is the
triangle number of . If we solve the
quadratic equation
In mathematics, a quadratic equation () is an equation that can be rearranged in standard form as
ax^2 + bx + c = 0\,,
where the variable (mathematics), variable represents an unknown number, and , , and represent known numbers, where . (If and ...
:
for as a function of , we get
:
which is a strictly increasing and continuous function when is non-negative real. Since
:
we get that
:
and thus
:
where is the
floor function
In mathematics, the floor function is the function that takes as input a real number , and gives as output the greatest integer less than or equal to , denoted or . Similarly, the ceiling function maps to the least integer greater than or eq ...
.
So to calculate and from , we do:
:
:
:
:
Since the Cantor pairing function is invertible, it must be
one-to-one and
onto
In mathematics, a surjective function (also known as surjection, or onto function ) is a function such that, for every element of the function's codomain, there exists one element in the function's domain such that . In other words, for a f ...
.
Examples
To calculate :
:,
:,
:,
:,
:,
so .
To find and such that :
:,
:,
:,
:,
:,
:,
so ;
:,
:,
:,
so ;
:,
so ;
:,
so ; thus .
Derivation

The graphical shape of Cantor's pairing function, a diagonal progression, is a standard trick in working with
infinite sequence
In mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Like a set, it contains members (also called ''elements'', or ''terms''). The number of elements (possibly infinite) is call ...
s and
countability
In mathematics, a set is countable if either it is finite or it can be made in one to one correspondence with the set of natural numbers. Equivalently, a set is ''countable'' if there exists an injective function from it into the natural numbers ...
. The algebraic rules of this diagonal-shaped function can verify its validity for a range of polynomials, of which a quadratic will turn out to be the simplest, using the
method of induction. Indeed, this same technique can also be followed to try and derive any number of other functions for any variety of schemes for enumerating the plane.
A pairing function can usually be defined inductively – that is, given the th pair, what is the th pair? The way Cantor's function progresses diagonally across the plane can be expressed as
:
.
The function must also define what to do when it hits the boundaries of the 1st quadrant – Cantor's pairing function resets back to the x-axis to resume its diagonal progression one step further out, or algebraically:
:
.
Also we need to define the starting point, what will be the initial step in our induction method: .
Assume that there is a quadratic 2-dimensional polynomial that can fit these conditions (if there were not, one could just repeat by trying a higher-degree polynomial). The general form is then
:
.
Plug in our initial and boundary conditions to get and:
:
,
so we can match our terms to get
:
:
:.
So every parameter can be written in terms of except for , and we have a final equation, our diagonal step, that will relate them:
:
Expand and match terms again to get fixed values for and , and thus all parameters:
:
:
:
:.
Therefore
:
is the Cantor pairing function, and we also demonstrated through the derivation that this satisfies all the conditions of induction.
Shifted Cantor pairing function
The following pairing function:
, where
. is the same as the Cantor pairing function, but shifted to exclude 0 (i.e.,
,
, and
). It was used in the popular computer textbook of Hopcroft and Ullman (1979).
Other pairing functions
The function
is a pairing function.
In 1990, Regan proposed the first known pairing function that is computable in
linear time
In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations ...
and with constant space (as the previously known examples can only be computed in linear time if
multiplication can be too, which is doubtful). In fact, both this pairing function and its inverse can be computed with finite-state transducers that run in real time. In the same paper, the author proposed two more monotone pairing functions that can be
computed online in linear time and with
logarithmic space; the first can also be computed offline with zero space.
In 2001, Pigeon proposed a pairing function based on
bit-interleaving, defined recursively as:
:
where
and
are the
least significant bits of ''i'' and ''j'' respectively.
In 2006, Szudzik proposed a "more elegant" pairing function defined by the expression:
:
Which can be unpaired using the expression:
:
(Qualitatively, it assigns consecutive numbers to pairs along the edges of squares.) This pairing function orders
SK combinator calculus expressions by depth.
This method is the mere application to
of the idea, found in most textbooks on Set Theory,
[See for instance .]
used to establish
for any infinite cardinal
in
ZFC.
Define on
the binary relation
:
is then shown to be a well-ordering such that every element has
predecessors, which implies that
.
It follows that
is isomorphic to
and the pairing function above is nothing more than the enumeration of integer couples in increasing order.
Citations
Notes
Footnotes
References
*
*
*
*
*
*
*
* {{cite book, title=Mathematics: The Man-Made Universe , last=Stein , first=Sherman K. , edition=3rd , publisher=Dover , year=1999 , isbn=9780486404509
Set theory
Georg Cantor
Functions and mappings