1

I'm not sure if the title is the correct description. What I want to do is basically like this: For example, if I have a binary sequence of 3-bits, I can have the following permutations:

1 -> [0,0,0]

2 -> [0,0,1]

3 -> [0,1,0]

4 -> [0,1,1]

5 -> [1,0,0]

6 -> [1,0,1]

7 -> [1,1,0]

8 -> [1,1,1]

But instead of binary sequence, I have a decimal array of integers upper bounded by some value N. Each of the digits can go from 0 to N.

So something like this:

1 -> [0,0,0]

2 -> [0,0,1]

3 -> [0,0,2]

...

i -> [0,0,N]

i+1 -> [0,1,0]

...

...

Is there a closed-form solution to find the digits of any n-th permutation? Even a recursive solution? Or is iterative the only option?

madu
  • 113

1 Answers1

3

What you are really doing is converting a number to base $N+1$. If your number is $k$, divide it by $N+1$ getting a quotient $q$ and remainder $r$. The last character in your string is $r$. Now continue by dividing $q$ by $N+1$. The remainder is the next to last character and keep going with the quotient. When the quotient becomes $0$ stop.

Ross Millikan
  • 374,822