**Notes:**

Using the Eclipse IDE, I have created a Java project that implements what is discussed in this post. You can view or download it from my github repository.

For convenience, this article includes the number 0 in the set of natural numbers, , which by convention usually does not include the number 0.

**Definition**: A set S is a *countable set* if and only if there exists a bijective mapping , where is the set of natural numbers

(i.e. for any pair of elements, a and b in S for which a b, , and for any element, n in , there exists an element c in S, such that ).

By creating a table with rows and columns both labeled by the natural numbers in ascending order, and with entries consisting of these ordered row and height number pairs, each entry can be counted while traversing along the diagonals of the table in such a manner as to yield the following sequence listing the elements of :

The reason for traversing along the diagonals of the table instead of its rows or columns is that, although they grow larger as the process continues, the diagonals are always finite in length. If I needed to count up to a particular ordered pair, I could always reach it in a finite number of steps by counting along the table’s diagonals. On the other hand, the rows and columns of the table are infinite in length. Were I to count along them, I could never get to an element occurring in a subsequent row or column.

Clearly, every possible ordered pair of natural numbers occurs in this sequence exactly once. Since there are an infinite number of possible ordered pairs of natural numbers, the sequence must be infinite in length. In this way, the set satisfies the definition of a countable set (a similar argument demonstrates that , the set of rational numbers, is also a countable set).

Repeating this process, the elements of can be counted by constructing a table with rows in and with columns in

which gives the sequence of elements in :

This sequence can in turn be crossed with once more to count the elements of , and so on.

If I am given a particular n-tuple, can I find its sequence index number without counting up to it? For instance, at what index in does the 3-tuple (13, 5, 7) occur? Obversely, if I am given a particular index number, can I find its corresponding n-tuple? for instance what is the 47th element in the sequence?

It is best to first solve the simplest case in , and then generalize to

As elements in are two-dimensional, this suggests a function of two variables, . As it turns out, it suffices that be quadratic.

Thus, with as yet unknown constants ,

As there are six unknown constants, a system of six equations suffices to determine what they are. We know that (0,0) is the zeroth element in , (1,0) is the 1st element, (0,1) is the 2nd, and so on…

Solving the system,

gives

and so,

This is known as the Cantor pairing function.

Not only can this function give the index of a particular 2-tuple, but by composing it recursively, it can give the index of a general n-tuple.

Now I can find the index of (13, 5, 7) in :

What about the inverse of this function, ? Given an index, can I calculate its corresponding n-tuple?

I will first show how to begin with a particular index in , i, and find the 2-tuple, (x(i),y(i)), that it corresponds to. Observe that the first element of the nth diagonal always begins with (n,0). Now also observe that the nth diagonal of the table has n+1 elements. For instance, the 0th diagonal has one element, the 1st diagonal has 2 elements, the 2nd diagonal has 3 elements, and so on. This suggests the following mappings:

If the ith element is also the th element, for some n, then

If i is larger than the th element, but still in the same diagonal, and hence, at most the th element

I can find the x(i) coordinate by substituting for n, this expression into . The y(i) coordinate is derived similarly.

I can extend the use of these equations to n-tuples by composing them in the following manner.

Now I can answer the second question, what is the 47th element in the sequence?

I have created a Java class, RecursiveIntTupleGenerator, that generates these sequences. An instance of this class has a specified number of dimensions, and is initialized to the 0th element of its corresponding sequence. You can view/download the Eclipse project source here.

As discussed above, the Cantor pairing function directly associates the natural numbers to the respective natural number n-tuple, and vice versa, but I was also interested in capturing the recursive nature of the n-tuple counting process. This is implemented in the next() method.

public void next() { if (dimensions == 1) { x += 1; } else { if (x > 0) { x -= 1; subTuple.next(); } else { savedX += 1; x = savedX; subTuple.setZero(); } } }

I’d like to expand on this project in the future. It must be possible to iterate over the integers (including negative numbers) in multiple dimensions just as well as it is possible to iterate over the natural numbers. Also, since this procedure may theoretically carry on indefinitely, it would make more sense to use Java’s BigInteger class instead of just integers.

Being able to generate a sequence of every possible positive whole number n-tuple affords an easy, brute-force solution search for multivariate Diophantine equations, and various other problems in number theory. I put my Java class to the test and verified Lagrange’s four-square theorem for the first 100 whole numbers.

I let my program run longer and verified the theorem for numbers up to 5,000. My program took a much, much longer time to verify the solution, than it did for any other number in this range. It must take many calls of the next() function to find this solution. Starting from (0,0,0,0), how many times does the next() function have to be called in order to find these four squares that add up to 3,072?