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.

6
  • stackoverflow.com/questions/5506888/… Commented Mar 29, 2013 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. Commented Mar 29, 2013 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 Commented Mar 29, 2013 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. Commented Mar 29, 2013 at 17:16
  • you can always order them once they are in the database... Commented Mar 29, 2013 at 17:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

You can test this here: phpcodepad.com
Thanks for that, it's perfect. However - how can I separate each 2 digits with a "|", please? Sorry, I'm new to PHP.
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)
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..
@AqworldsDragon, Ok I've radically updated the answer to actually spit out data that you can insert or in this case print.
|
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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.