Ladies and gentlemen, I would like to generate an anagram of possible combinations. Ex: "123456" would generate: 1234 2341 6531 5431 1243 1342 or 12 43 56 23 14 16, whether or not repeated, and so on, I know how to get the number of combinations by factorial, but I do not know how to start the algorithm to generate the combinations, I've read quite a lot about combinatorial analysis, permutation, etc., but I still can not implement the algorithm. If anyone can only give me a light to start it would be GREAT! can be in pseudocode itself.
would pass a String and the size. Ex: where size would be ten hundred or thousands.
class Permutacao
{
public $resul;
private $cont;
function __construct()
{
$this->resul = array();
}
public function permuta($array, $indice)
{
if($indice == count($array)){
$this->cont++;
array_push($this->resul, $this->arraytemp);
}
else{
for ($i=0; $i < count($array); $i++) {
$valida = false;
for ($j=0; $j < $indice; $j++) {
if($this->arraytemp[$j] == $array[$i]) $valida = true;
}
if (!$valida) {
echo $indice." ";
$this->arraytemp[$indice] = $array[$i];
$this->permuta($array, $indice + 1);
}
}
}
}
}
echo "<pre>";
$permuta = new Permutacao();
$permuta->permuta(array(1,2,3,4), 0);
print_r($permuta->resul);
UPDATE: Searching found this simpler way to implement, my biggest difficulty is to understand the algorithm, as I pass the "$ index = 0" it gets to 3 and then returns to 1 etc., and if the array with repeated values it returns nothing! 1,1,2,3, it would have to return
1,1,3
1,1,2
2,1,1 etc.