0

How could I generate every possible number of a given amount of digits and using specific digits?

So basically, I would like to have a 6 digit number for example using only the numbers ['1','2','3']. I've tried a few methods of recursion, however, I can't get it to work correctly due to my other complication, which is adding a separator of "|" in between each 2 digits. So the list would be like so:

11|11|11
11|11|12
11|11|13
11|11|21
11|11|22
11|11|23

etc.. Would be appreciated if someone could point me in the right direction. Also a way of dumping each of the combinations into my MySQL database would be great.

Engine
  • 89
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/5506888/permutations-all-possible-sets-of-numbers – Hanky Panky Mar 29 '13 at 17:12
  • forget the separators you can always add them later. just select a random number (between 1 and 3) 6 times and concantenate it together. check your database table to see if it exists, if not add it. run it a bunch of times. eventually you will get them all. – nathan hayfield Mar 29 '13 at 17:13
  • Also that pipe separator is just symbolic, it has no importance for the permutations algorithm so do not consider it a `complexity` – Hanky Panky Mar 29 '13 at 17:14
  • This isn't a duplicate, I can't do what I require with them, I've looked through a few questions and still can't manage to get it right. Also, I may need to expand it to around 10 digits between the separators, so I can't just do that + the fact I don't think that would order them properly. – Engine Mar 29 '13 at 17:16
  • you can always order them once they are in the database... – nathan hayfield Mar 29 '13 at 17:16
  • There's only 729 of them. Here. http://pastebin.com/7Vq3qaaU – 000 Mar 29 '13 at 17:39

2 Answers2

2

Here is a much updated answer (originally updated from this answer]1) to your question:

function findPermutations($arr, $arrLen, $size, $perArr = array(), $pos = 0, &$found = array()) {
   if ($size==$pos) { //if $pos reach $size then we have found one permutation
      $found[] = vsprintf("%s%s|%s%s|%s%s", $perArr);
      return;
   }
   for ($i=0; $i<$arrLen; $i++) {

      $perArr[$pos] = $arr[$i]; //put i'th char in current position
      //The recursive call that move to next position with $pos+1
      findPermutations($arr, $arrLen, $size, $perArr, $pos+1, $found); 
   }
   return $found;
}

$permutations = array();
$letters = array('1','2','3');
$max_length = 6;

$permutations = findPermutations($letters, count($letters), $max_length);

for($i = 0; $i < count($permutations); $i++) {
    print ($permutations[$i].'<br/>');
}

Here is what I'm doing. I'm passing in an empty array called $permutations by reference, and as I find new permutations, I'm appending them to it. When the function findPermutations() is complete, I end up with an array of all permutation, that I can iterate over or insert. To get the formatting I'm using vsprintf, that lets me pass an array of data and apply a format (in this case %s%s|%s%s|%s%s). Lastly I'm using default argument values to make calling this function cleaner.

Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • You can test this here: http://phpcodepad.com/ – Jason Sperske Mar 29 '13 at 17:22
  • Thanks for that, it's perfect. However - how can I separate each 2 digits with a "|", please? Sorry, I'm new to PHP. – Engine Mar 29 '13 at 17:36
  • Thous lines above the `return` statement are for printing out each permutation, you can change them to something like `print $perArr[0].$perArr[1].'|'.$perArr[2].$perArr[3].'|'.$perArr[4].$perArr[5]` or that could eb where you add your MySQL INSERT command (I'd rename it storePermutation if you did that) – Jason Sperske Mar 29 '13 at 17:39
  • Thanks a lot for this, it's perfect, except I've just added them spacers and it prints the same set around 10 times before the next one? 11-11-11 is printed 10 times before 11-11-12 is.. – Engine Mar 29 '13 at 19:46
  • @AqworldsDragon, Ok I've radically updated the answer to actually spit out data that you can insert or in this case print. – Jason Sperske Mar 29 '13 at 22:09
  • Thanks for that, it's actually perfect! Now I just need a server to run my script on for the bigger ones haha – Engine Mar 31 '13 at 22:07
0

you mean something like this?

$letters='123'; // add other numbers

for($i=0;$i<3;$i++) { //generate 3 pairs
  $pairs[]=$letters[rand(0,2)] . $letters[rand(0,2)];
}
//then join them together
$finalstring=implode('-',$pairs);
Youn Elan
  • 2,379
  • 3
  • 23
  • 32