## The Cantor Pairing Function

The objective of this post is to construct a pairing function, that presents us with a bijection between the set of natural numbers, and the lattice of points in the plane with non-negative integer coordinates.

We will accomplish this by creating the corresponding map (and its inverse), that takes each natural number and drops it at a location in the lattice, as the following diagram suggests:

That is: the zero goes at the origin, and then we populate the two positions in the diagonal . Then we populate the three positions in the diagonal , and so on. Note that by doing so, when we are done populating the last positions of the –th diagonal, we have placed natural numbers (including the zero!) in the lattice.

In particular, note that the triangular numbers occupy all the positions in the –axis, precisely where this one intersects the lines

This is the key ingredient to construct the required bijection: Given a natural number there is a **unique** value such that (since the sequence of triangular numbers is strictly increasing). The number then belongs to the intersection of the lattice with the diagonal line and it only remains to know how far from the –axis: the **offset** actually indicates the coordinate of the position of in the lattice. We can now find the precise position of by solving the following simple system of two equations with two variables:

Or simply put, the coordinates are given by The question remaining is, of course, how to find the value of

We need to look for the value of such that

One way to accomplish this is to find the closest natural number to the solution of either side of the inequalities above, say By choosing this side, an appropriate expression for this value is given by

We have successfully computed for each natural number a unique position in the lattice The construction is also such that for each position in the previous lattice, there is a unique natural number that can be sent to that position. The corresponding expression will lead us to the construction of the inverse function The key is again the same system of equations as before:

Given we find This leads us to compute the corresponding and the second equations gives us

### Miscellaneous

Antoine Flattot suggests using this construction to find a proof that the natural numbers are also in bijection with the rational numbers, and thus they both have the same cardinality. How would the reader go about it?

Also, by using either function above we can obtain elegant diagrams in with the `tikz` package, showing graphically the structure of the location assignments. For example, with the choice of we could write a simple function to assign all positions from 0 to including arrows pointing the direction in which the sequence progresses:

\def\gCpf#1{ % Compute the maximum width of the box that contains all values % from 0 to #1 \pgfmathparse{0.5+floor(0.5*(sqrt(8*#1+1)-1))} \let\width\pgfmathresult % Generate a grid with the corresponding width \draw (-0.25cm,-0.25cm) grid[step=2cm,ultra thin,gray] (2*\width cm+0.25cm, 2*\width cm+0.25cm); \foreach \x in {0,...,\width} { \draw[gray,ultra thin] (2*\x,0.2cm-0.25cm) -- (2*\x, -0.2cm-0.25cm) node[below] {\large$\boldsymbol{\x}$}; \draw[gray,ultra thin] (0.2cm-0.25cm,2*\x) -- (-0.2cm-0.25cm,2*\x) node[left] {\large$\boldsymbol{\x}$}; % First node, always at the origin \filldraw(0,0) circle (2pt); \draw(10pt,8pt) node{\large$0$}; % For each number z between 1 and #1... \foreach \n in {1,..., #1} { % Compute the diagonal number n(z) and the previous \pgfmathparse{floor(0.5*(sqrt(8*\n+1)-1))} \let\thisn\pgfmathresult \pgfmathparse{floor(0.5*(sqrt(8*\n-7)-1))} \let\prevn\pgfmathresult %Compute the triangular number t_n and the previous \pgfmathparse{0.5*(\thisn^2+\thisn)} \let\thist\pgfmathresult \pgfmathparse{0.5*(\prevn^2+\prevn)} \let\prevt\pgfmathresult % Compute the y coords of this and the previous \pgfmathparse{\n-\thist} \let\thisy\pgfmathresult \pgfmathparse{\n-1-\prevt} \let\prevy\pgfmathresult % Compute the x coords of this and the previous \pgfmathparse{\thisn-\thisy} \let\thisx\pgfmathresult \pgfmathparse{\prevn-\prevy} \let\prevx\pgfmathresult % Done. Let's place the nodes at their locations \node (this) at (2*\thisx,2*\thisy) [inner sep=6pt] {}; \node (prev) at (2*\prevx,2*\prevy) [inner sep=6pt] {}; \draw (2*\thisx cm+10pt,2*\thisy cm+8pt) node{\large$\n$}; \filldraw (2*\thisx cm,2*\thisy cm) circle (2pt); % If the node belongs in the x-axis, receive a red arrow % Otherwise, a black arrow \ifthenelse{\lengthtest{\thisy cm = 0 cm}}{ \draw[->,thick,red] (prev) -- (this); }{ \draw[->] (prev) -- (this); } }

Note that the libraries `calc` and `ifthen` need to be loaded previously, to be able to handle some of the commands.

Nice and thanks!

How do you use this code? I am compiling it (putting the code inside the begindocument), but it is not working. Is it possible you link to a file which can be directly compiled?

The code creates a function of one variable. This variable controls how many integers to show. Put that function in your preamble, make sure that the packages

tikz,calcandifthenare loaded, and call the function with any value inside of your document. Something like this:Thank you. It works great.

de res

Very nice! I am writing a set of notes, and I would like to include a graph produced using this code. Would this be possible, and if so, is there anything specific you’d like me to include in the acknowledgments (besides your name and a link to this entry)?

Thanks! Feel free to use anything you see in my blog. As far as I am concerned, everything I post is open source. You don’t need to cite or acknowledge if you don’t want, either.

The only request: if you can improve my code, or get something even better-looking, or a nice application, I would love to hear about it.

Many thanks!