Artificial intelligent assistant

Generate a unique combination from an index within the number of combinations I'm writing a program which will use a genetic algorithm optimize neural networks to play tic-tac-toe (That's not related), and I've come across the following problem: I'm looping through every permutation of a tic-tac-toe game ($3^9$ or 362880) and from that number, I want to generate one of those $3^9$ combinations. I think it might be possible using bases, however, ideally (and reasonably) the 362880th permutation should be: 2 | 2 | 2 --------- 2 | 2 | 2 --------- 2 | 2 | 2 However, 362880 in base 3 isn't even 9 digits long (it's 12: `200102210000`). Anyway, the questions is: **Given a number n, between $1$ and $3^9$ inclusive, is it possible, and if so, how, to generate a unique set of length $9$ with values of either $0$, $1$ or $2$ (i.e. base 3)** Any help is much appreciated, Brendan

$3^9$ is actually $19683$ which is $1000000000$ in base $9$.

The numbers in the range $0,3^9-1$ all have an expression of length $9$ in base $3$.

What you want to do is label the spots in the tick tack toe as follows:


9 | 8 | 7
---------
6 | 5 | 4
---------
3 | 2 | 1


And then, assign to every number in the range $0,3^n-1$ a tick tack toe board depending on its base $3$ expression.

So for example $1234$ is $001200201$ in base $3$. So we assing the following board to $1234$:


0 | 0 | 1
---------
2 | 0 | 0
---------
2 | 0 | 1

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 202d3ba836ff41fe6c92a83af261614c