0

im inspired by this post:

PHP algorithm to generate all combinations of a specific size from a single set

im using the following code snippet:

function comb ($n, $elems) {
    if ($n > 0) {
      $tmp_set = array();
      $res = comb($n-1, $elems);
      foreach ($res as $ce) {
          foreach ($elems as $e) {
             array_push($tmp_set, $ce . $e);
          }
       }
       return $tmp_set;
    }
    else {
        return array('');
    }
}
$elems = array('A','B','C', 'a', 'b', 'c', 0, 1, 2, 3);
$v = comb(7, $elems);

This works nice, but the problem is, that it creates me combinations like this:

(A, B, a) (A, B, C) (A, B, C, 0) (A, B, C, 1, 2)

i want to skip all these combinations of 3 for, i just want all combinations of 7 digits, fpr example:

(A, B, C, 0, 1, 2, a) (A, B, C, 0, 1, 2, B)

and so on ...

How could i tweak this code,

Thank you for your help!

Community
  • 1
  • 1
redigaffi
  • 429
  • 6
  • 22

1 Answers1

0

Your code works fine for me. Try running it with just three elements and you'll see that it only outputs combinations of length 7. Don't forget to print_r($v) at the end.

Neal
  • 912
  • 8
  • 14
  • Hello, thanks for your help, ive seen it and got it working. The problem is that my dataset has 64 values, and my n is 20, so i will have a lot lot lot lot lot lot lot combinations.... The problem is, that this will need years to compute all combinations, is there a way to reduce the time or something i can do? – redigaffi Nov 09 '16 at 22:30
  • You can't because 64 choose 20 is 1.9619726 * 10^16, so generating that list will take a long time no matter how you do it. If there's something else you want to do that doesn't involve generating the whole list though, you may be able to do it faster. – Neal Nov 10 '16 at 20:06