 #jsDisabledContent { display:none; } My Account |  Register |  Help Flag as Inappropriate This article will be permanently flagged as inappropriate and made unaccessible to everyone. Are you certain this article is inappropriate?          Excessive Violence          Sexual Content          Political / Social Email this Article Email Address:

# Cantor pairing function

Article Id: WHEBN0002392322
Reproduction Date:

 Title: Cantor pairing function Author: World Heritage Encyclopedia Language: English Subject: Collection: Publisher: World Heritage Encyclopedia Publication Date:

### 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.

## Definition

A pairing function is a primitive recursive bijection

$\pi:\mathbb\left\{N\right\} \times \mathbb\left\{N\right\} \to \mathbb\left\{N\right\}.$

## Cantor pairing function

The Cantor pairing function is a pairing function

$\pi:\mathbb\left\{N\right\} \times \mathbb\left\{N\right\} \to \mathbb\left\{N\right\}$

defined by

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^\left\{\left(n\right)\right\}:\mathbb\left\{N\right\}^n \to \mathbb\left\{N\right\}$

as

### Inverting the Cantor pairing function

Suppose we are given z with

$z = \langle x, y \rangle = \frac\left\{\left(x + y\right)\left(x + y + 1\right)\right\}\left\{2\right\} + 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\left\{w\left(w + 1\right)\right\}\left\{2\right\} = \frac\left\{w^2 + w\right\}\left\{2\right\}$
$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\left\{\sqrt\left\{8t + 1\right\} - 1\right\}\left\{2\right\}$

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

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

we get that

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

and thus

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

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

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

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

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.