0

I ran across this question this morning.

Basically that question is about data which has to create permutations for 6 values; each one ranging from 1 to 38.

So, first permutation would be

1 1 1 1 1 1      [ permutation 1 ]
1 1 1 1 1 2      [ permutation 2 ]
1 1 1 1 1 3...   [ permutation 3 ]

to end much later with

38 38 38 38 38 38 [ permutation 38^^6 ]

The output is simply created by 6 nested loops, each counting from 1 to 38; and within the inner-most loop, you print the 6 loop counters.

Now I am wondering about the math behind that; and out of curiosity: what would be the "function" that

  1. computes the "permutation index", given a any permutation 1 2 3 4 5 6
  2. Probably more interesting: that takes an "index", such as 102382; and tells me the corresponding permutation output

Any idea anybody?

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248

1 Answers1

3

It works exactly like a change of base (binar, octal or hex). The first one question simply: 1*38^6 + 2*38^5 + 3*38^4 + 4*38^3 +...+6*38^0 The second one reversed: 102382 mod 38... recursively UPDATE Let us assume we want change 10 to base 2:

10/2=5 remainder(modulus)  **0** 
5/2=2 remainder           **1**
2/2=1 remainder           **0**
1/2=0 remainder           **1**

backwards is 1010 general gave a M to change in base B, just divide M by B , and the remainder are going to be the digit in the new base

jurhas
  • 613
  • 1
  • 4
  • 12
  • I am kinda slow today; if you don't mind rolling out that "reverse" a bit more. Will probably result in not only upvoting but also accepting your answer then ;-) – GhostCat Sep 08 '16 at 13:57
  • It is the algorithm for change of base. Let's assume we want convert 9 to binary. The string get built backwards. So we start: – jurhas Sep 09 '16 at 06:57