Bijective function from counting number to element of n-ary Cartesian product without enumeration Assume a Cartesian product $X$ calculated over $N$ sets, where $X$ has some cardinality $c$.
I wish to find a bijective function that maps from a counting number ($i$ such that $0 \le i \lt c$) to each element of $X$.
The trouble is that I am in an engineering situation where the memory requirements are such that I cannot precompute $X$. If this were not the case I could simply iteratively produce $X$ and access each of the elements in it by its index.
Rethinking the problem
Perhaps I am thinking about my problem incorrectly as well, so be explicit: I have access to the $N$ sets and their elements, and need a way to select each combination with just an index number. The Cartesian product and bijective function seemed like the best way to formulate this, thus the above question.
The problem is very similar to this one, but again I cannot iteratively calculate this and need to map directly from $i$ to an element of $X$.
 A: Let us say your $N$ sets are $X_0,\dots,X_{N-1}$, and $X_i$ has $c_i$ elements for each $i$.  Then you can construct a bijection $f:\{0,\dots,c-1\}\to X$ as follows.  Given $a\in \{0,\dots,c-1\}$, you can write $$a=\sum_{i=0}^{N-1}a_ic_0\dots c_{i-1}$$ for a unique sequence of integers $a_0,\dots,a_{N-1}$ where $a_i\in\{0,\dots,c_i-1\}$ for each $i$.  Then define $f(a)$ to be the element of $X$ whose $i$th coordinate is the $a_i$th element of $X_i$ for each $i$.
If this looks mysterious, consider the example where each $X_i$ has $10$ elements, so $c_i=10$ for each $i$.  Then the representation of $a$ above becomes just $a=\sum a_i10^i$ where each $a_i$ is between $0$ and $9$, which is nothing other than the decimal expansion of $a$.  So you are mapping $a$ to the element of $X$ corresponding to its sequence of digits in base $10$.  In general, the idea is similar, except instead of a single fixed base, you are representing $a$ in a "mixed base" where each digit is in a different base (namely, the $i$th digit is in base $c_i$).
Here's one simple way you can implement this algorithmically.  Given $a$, you can compute $a_0$ as the remainder when $a$ is divided by $c_0$.  Let $a'=\frac{a-a_0}{c_0}$ (i.e., the integer quotient when $a$ is divided by $c_0$).  Then $a_1$ can be obtained as the remainder when $a'$ is divided by $c_1$.  Similarly you can then take $a''=\frac{a'-a_1}{c_1}$ and obtain $a_2$ as the remainder when $a''$ is divided by $c_2$, and so on.
