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.

$$\pi\colon \mathbb{N} \cup \{ 0 \} \to \big( \mathbb{N} \cup \{ 0 \} \big)^2.$$

We will accomplish this by creating the corresponding map (and its inverse), that takes each natural number $$z$$ 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 $$x+y=1$$. Then we populate the three positions in the diagonal $$x+y=2$$, and so on. Note that by doing so, when we are done populating the last $$n+1$$ positions of the $$n$$–th diagonal, we have placed $$1+2+3+\dotsb+(n+1) = \tfrac{(n+1)(n+2)}{2}$$ natural numbers (including the zero!) in the lattice.

In particular, note that the triangular numbers $$t_n = 1 + 2 + \dotsb + n$$ occupy all the positions in the $$x$$–axis, precisely where this one intersects the lines $$x+y=n$$.

This is the key ingredient to construct the required bijection: Given a natural number $$z \in \mathbb{N},$$ there is a unique value $$n = n(z)$$ such that $$t_n \leq z < t_{n+1}$$ (since the sequence of triangular numbers is strictly increasing). The number $$z$$ then belongs to the intersection of the lattice with the diagonal line $$x+y=n,$$ and it only remains to know how far from the $$x$$–axis: the offset $$z-t_n$$ actually indicates the $$y$$ coordinate of the position of $$z$$ in the lattice. We can now find the precise position of $$z$$ by solving the following simple system of two equations with two variables:

$$\left\{ \begin{array}{rl} x+y &= n(z)\\ y &= z - t_{n(z)} \end{array}\right.$$

Or simply put, the coordinates are given by $$x=n(z)-z+t_{n(z)}$$, $$y=z-t_{n(z)}.$$ The question remaining is, of course, how to find the value of $$n(z).$$

We need to look for the value of $$n$$ such that

$$\displaystyle{\frac{n(n+1)}{2}} \leq z < \displaystyle{\frac{(n+1)(n+2)}{2}}.$$

One way to accomplish this is to find the closest natural number $$n$$ to the solution of either side of the inequalities above, say $$n^2 + n -2z = 0.$$ By choosing this side, an appropriate expression for this value is given by

$$n(z) = \big\lfloor \frac{-1+\sqrt{1+8z}}{2} \big\rfloor.$$

We have successfully computed for each natural number $$z \in \mathbb{N} \cup { 0 }$$ a unique position in the lattice $$\big( \mathbb{N} \cup { 0 }\big)^2.$$ The construction is also such that for each position $$(x,y)$$ 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 $$\pi^{-1} \colon \big( \mathbb{N} \cup { 0 } \big)^2 \to \mathbb{N} \cup { 0 }.$$ The key is again the same system of equations as before:

Given $$(x,y),$$ we find $$n=x+y.$$ This leads us to compute the corresponding $$t_n,$$ and the second equations gives us

$$z = y + t_n = y + \displaystyle{\frac{n(n+1)}{2}} = \underbrace{y + \displaystyle{\frac{(x+y)(x+y+1)}{2}}}_{\pi^{-1}(x,y)}.$$

### 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 $$\LaTeX$$ with the tikz package, showing graphically the structure of the location assignments. For example, with the choice of $$\pi,$$ we could write a simple function to assign all $$z+1$$ positions from 0 to $$z,$$ including arrows pointing the direction in which the sequence progresses:

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