I need an algorithm that manages all possible arrangements for n elements in n positions, that is, n! is the amount of possible arrangements, as in the example below:
n = 3
3! = 6 arranjos
123
132
213
231
312
321
I'm trying to create it in JavaScript, but it's not working.
function partida(){
var nc = 4;
var nl = fatorial(nc);
var jogada = new Array(nl);
for (i = 0; i < nl; i++){
jogada[i] = new Array(nc);
}
for (c = 0; c < nc; c++){
var q = fatorial(nc - c)/(nc - c);
for (l = 0; l < nl; l++){
jogada[l][c] = Math.trunc(l/q) + c;
}
}
textarea = document.getElementById('arranjos');
for (l = 0; l < nl; l++){
textarea.innerHTML += (jogada[l].toString()).replace(/,/g, "");
textarea.innerHTML += '\n';
}
}