# Finding the Nth number in a generated list

I am generating numbers as follows:

Let the first digit range from 1 to 2 inclusive. Let the second digit range from 1 to 3 inclusive. Let the last digit range from 1 to 2 inclusive.

I am then generating all the possible numbers by cycling the last digit, then the middle, then the first to get this:

1 1 1
1 1 2
1 2 1
1 2 2
1 3 1
1 3 2
2 1 1
2 1 2


and so on.

How would you find the Nth number in the list generated using this method. For example, querying for the 2nd number would return 111, while querying for the 3rd would return 121.

• abc corresponds to $6(a-1)+2(b-1)+c$. Commented Nov 17, 2013 at 5:35

The first digit is $\lceil \frac{N}{6}\rceil$, where $\lceil \cdot \rceil$ denotes the ceiling function.
The second digit is $\lceil \frac{N \pmod 6}{2}\rceil$, where $\pmod{6}$ denotes modular arithmetic.
The third digit is $2-(N \pmod{2})$.
• If the range for the 2nd digit is $1..x$ and the range for the 3rd digit is $1..y$, then replace $6$ by $xy$ in both places. In the 2nd formula, replace $2$ by $y$. The 3rd formula is kind of a trick, what you want is just $N \pmod{y}$, but if the answer is $0$ you add $y$. For $y=2$ you can do it in one step via my formula. Commented Nov 17, 2013 at 6:03
• Ok - did that. Nonetheless, would the pattern continue so that if there were 4 digits, the first digit would be $\left\lceil \frac{N}{xyz} \right\rceil$ where x,y,z is the range of the second, third, and fourth digits. Would the second digit then be $\left\lceil \frac{N\; \left( \mbox{mod}\; xyz \right)}{xy} \right\rceil$ and so on? Commented Nov 17, 2013 at 7:27
• I got a response in the other thread that said the formula was $\left\lceil \frac{N}{\prod_{x\; =\; 0}^{x\; =\; i}{\mbox{S}_{i}}} \right\rceil$ where S_i is the range of the wanted digit, N. This formula lacks the modulo yours has though. Is the formula incorrect? Commented Nov 17, 2013 at 20:24