1
$\begingroup$

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.

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

1 Answer 1

1
$\begingroup$

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})$.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – vadim123
    Commented Nov 17, 2013 at 6:03
  • $\begingroup$ Got it! What if there were more than 3 digits though? How would you modify the formulas then? $\endgroup$
    – 1110101001
    Commented Nov 17, 2013 at 6:07
  • $\begingroup$ Perhaps you should ask these as separate questions. $\endgroup$
    – vadim123
    Commented Nov 17, 2013 at 6:37
  • $\begingroup$ 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? $\endgroup$
    – 1110101001
    Commented Nov 17, 2013 at 7:27
  • $\begingroup$ 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? $\endgroup$
    – 1110101001
    Commented Nov 17, 2013 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .