Given an array of N elements representing the permutation atoms, is there an algorithm like that:

```
function getNthPermutation( $atoms, $permutation_index, $size )
```

where `$atoms`

is the array of elements, `$permutation_index`

is the index of the permutation and `$size`

is the size of the permutation.

For instance:

```
$atoms = array( 'A', 'B', 'C' );
// getting third permutation of 2 elements
$perm = getNthPermutation( $atoms, 3, 2 );
echo implode( ', ', $perm )."n";
```

Would print:

```
B, A
```

Without computing every permutation until $permutation_index ?

I heard something about factoradic permutations, but every implementation i've found gives as result a permutation with the same size of V, which is not my case.

Thanks.

As stated by RickyBobby, when considering the lexicographical order of permutations, you should use the factorial decomposition at your advantage.

From a practical point of view, this is how I see it:

`(n-1)!`

,`(n-2)!`

, and so on.`i`

-th quotient should be a number between`0`

and`n-i-1`

inclusive, where`i`

goes from`0`

to`n-1`

.isyour permutation. The problem is that each quotient does not care for previous values, so you need to adjust them. More explicitly, you need to increment every value as many times as there are previous values that are lower or equal.The following C code should give you an idea of how this works (

`n`

is the number of entries, and`i`

is the index of the permutation):For example,

`ithPermutation(10, 3628799)`

prints, as expected, the last permutation of ten elements: