Your guess is correct, this code builds the sequence that corresponds to the current permutation number. If you have an N numbers you can arrange them in N! (N factorial = 1*2*3*4*...*N) different ways.
As an example, let's assume we have four numbers [1, 2, 3, 4] (N = 4) which gives us 24 permutations. To get the permulation 17th permutation, for example, the code does the following:
There are four possibilities which number is chosen for the first spot in the sequence, which in this case is determined by which 'bracket' the permutation number falls into - 1-6, 7-12, 13-18, 19-24. As you might notice, the bracket size is 6 = 3! = (N-1)! which is what the "divisor" variable in the code is. By dividing our permutation number by it and rounding up to the nearest integer, we get the bracket number, in this case Math.ceil(17/6) = 3. So the third number in the base sequence will be the first number in this permutation.
To continue this until all numbers have been chosen, we now subtract from our permutation number, so that it now falls into the range [1, (N-1)!] and can be used as before but shifting N down by one. We also remove the chosen number from the base sequence and are thus left with [1, 2, 4].
The number in our example would become 17 - 6*(3-1) = 5. (This is equivalent to the modulo operation ((17-1)%6)+1) We continue by dividing this by 2!. Which is again 3, so we choose the third number in our remaining base sequence, the 4.
We subtract from the permutation number again and get 5 - 2*(3-1) = 1. So next we choose the first number and there is only one left.
The 17th permutation would thus be [3, 4, 1, 2]. Using this algorithm we get all possible permutations in a 'neat' order.
