World Library  
Flag as Inappropriate
Email this Article

Cantor pairing function

Article Id: WHEBN0002392322
Reproduction Date:

Title: Cantor pairing function  
Author: World Heritage Encyclopedia
Language: English
Subject: Countable set, Cylindrification
Publisher: World Heritage Encyclopedia

Cantor pairing function

In mathematics a pairing function is a process to uniquely encode two natural numbers into a single natural number.

Any pairing function can be used in set theory to prove that integers and rational numbers have the same cardinality as natural numbers. In theoretical computer science they are used to encode a function defined on a vector of natural numbers f:NkN into a new function g:NN.


A pairing function is a primitive recursive bijection

\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}.

Cantor pairing function

The Cantor pairing function is a pairing function

\pi:\mathbb{N} \times \mathbb{N} \to \mathbb{N}

defined by

\pi(k_1,k_2) := \frac{1}{2}(k_1 + k_2)(k_1 + k_2 + 1)+k_2.

When we apply the pairing function to k_1 and k_2 we often denote the resulting number as \langle k_1, k_2 \rangle \,.

This definition can be inductively generalized to the Cantor tuple function

\pi^{(n)}:\mathbb{N}^n \to \mathbb{N}


\pi^{(n)}(k_1, \ldots, k_{n-1}, k_n) := \pi ( \pi^{(n-1)}(k_1, \ldots, k_{n-1}) , k_n) \,.

Inverting the Cantor pairing function

Suppose we are given z with

z = \langle x, y \rangle = \frac{(x + y)(x + y + 1)}{2} + y

and we want to find x and y. It is helpful to define some intermediate values in the calculation:

w = x + y \!
t = \frac{w(w + 1)}{2} = \frac{w^2 + w}{2}
z = t + y \!

where t is the triangle number of w. If we solve the quadratic equation

w^2 + w - 2t = 0 \!

for w as a function of t, we get

w = \frac{\sqrt{8t + 1} - 1}{2}

which is a strictly increasing and continuous function when t is non-negative real. Since

t \leq z = t + y < t + (w + 1) = \frac{(w + 1)^2 + (w + 1)}{2}

we get that

w \leq \frac{\sqrt{8z + 1} - 1}{2} < w + 1

and thus

w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor .

So to calculate x and y from z, we do:

w = \left\lfloor \frac{\sqrt{8z + 1} - 1}{2} \right\rfloor
t = \frac{w^2 + w}{2}
y = z - t \!
x = w - y \!.

Since the Cantor pairing function is invertible, it must be one-to-one and onto.


  • Steven Pigeon, "MathWorld.
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.