[ home ] [ math / cs / ai / phy / as / chem / bio / geo ] [ civ / aero / mech / ee / hdl / os / dev / web / app / sys / net / sec ] [ med / fin / psy / soc / his / lit / lin / phi / arch ] [ off / vg / jp / 2hu / tc / ts / adv / hr / meta / tex ] [ chat ] [ wiki ]

Viewing source code

The following is the source code for post >>>/math/481

Do you know of any interesting ways to list all possible \math{k}-tuples of nonnegative integers in order? Or in other words, do you know a function \math{f(n)} such that for every \math{(a, b)} with integers \math{a, b \ge 0}, there exists an \math{n \ge 0} with \math{f(n) = (a, b)}? Well, I do! I'll be showing my way.

First you create a grid with two axes which you start numbering starting at 0. The square in the \math{p}th row and \math{q}th column will have the tuple \math{(p - 1, q - 1) = (a, b)}. Not \math{(n, m)} because we start at 0. Now, we draw a line that connects the squares like in the left grid of the image attached (translucent, red line). The line creates this one-dimensional sequence of 2-tuples: \math{(0, 0) \rightarrow (0, 1) \rightarrow (1, 0) \rightarrow (2, 0) \rightarrow (1, 1) \rightarrow (0, 2) \rightarrow (0, 3) \rightarrow (1, 2) \rightarrow (2, 1) \rightarrow (3, 0) \rightarrow (4, 0) \rightarrow \dots} You should notice the pattern. Like that, I've listed all 2-tuples.

We define \math{f_k(n)} as our function that maps \math{\lbrace 0, 1, 2, \ldots \rbrace} to the set of all \math{k}-tuples of nonnegative integers.

We can extend this to 3-tuples. But without using a 3-dimensional grid. We make a similar grid (right side of image), this time the upper axis is for the third element in the tuple and the left axis is just the sequence of 2-tuples I listed before for the 1st and 2nd elements of the 3-tuple.

Using that method, we get this for \math{k = 3}:
\begin{math}
f_3(0) = (0, 0, 0)\\
f_3(1) = (0, 0, 1)\\
f_3(2) = (0, 1, 0)\\
f_3(3) = (1, 0, 0)\\
f_3(4) = (0, 1, 1)\\
f_3(5) = (0, 0, 2)\\
\ \ \ \vdots
\end{math}

A slightly less intuitive pattern...

Of course, you can generalize this method to get the formula: \math{f_{k+1}(n) = f_k(b(n)) \cup s(n)}. The union symbol is used here to append \math{s(n)} to to the \math{k}-tuple \math{f_k(b(n))}. \math{b(n)} refers to the first element of \math{f_2(n)} and \math{s(n)} refers to the second. Also, it isn't particularly hard to prove that these functions indeed do return every possible tuple.

I find this formula beautiful. Correct me in case you find mistakes!