Jump to content

Recommended Posts

Hello,

 

I have a situation where I want to match possible combinations of a number from a list of numbers, here is an example:

 

The main number, let's call it mainnum = 80 (this is variable and can change).

 

The set of numbers from which I want to match combinations is (these are constants and do not change):

1. A=10

2. B=20

3. C=15

4. D=25

5. E=40

6. F=50

7. G=5

 

Now, the possible combinations to match with mainnum, i.e. 80 are:

1. A+B+F

2. C+D+E

3. D+F+G

4. B+C+E+G

5. And so on

 

This is want I want to extract, i.e. possible combinations of mainnum from a list of constant numbers.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/286957-finding-possible-combinations/
Share on other sites

The programmer in ME wouldn't let this go without a stab at it.

$nums = array("A"=>10,"B"=>20,"C"=>15,"D"=>25,"E"=>40,"F"=>50,"G"=>5);
$target = 80;
$results = array();
foreach($nums as $k1=>$v1)
{
 if ($v1 == $target)
 {
  $keys = $k1;
  $results[$keys] = $v1;
 }
 else
 {
  foreach($nums as $k2=>$v2)
  {
   if ($k2==$k1)
    continue;
   else
   {
    if ($v1+$v2 == $target)
    {
     $keys = array($k1,$k2);
     sort($keys);
     $keys = implode('',$keys);
     if (array_key_exists($keys,$results))
      continue;
     $results[$keys] = "($k1) $v1 + ($k2) $v2";
    }
    else
    {
     foreach($nums as $k3=>$v3)
     {
      if ($k3==$k1 || $k3==$k2)
       continue;
      else
      {
       if ($v1+$v2+$v3 == $target)
       {
        $keys = array($k1,$k2,$k3);
        sort($keys);
        $keys = implode('',$keys);
        if (array_key_exists($keys,$results))
         continue;
        $results[$keys] = "($k1) $v1 + ($k2) $v2 + ($k3) $v3";
       }
       else
       {
        foreach($nums as $k4=>$v4)
        {
         if ($k4==$k1 || $k4==$k2|| $k4==$k3)
          continue;
         else
         {
          if ($v1+$v2+$v3+$v4 == $target)
          {
           $keys = array($k1,$k2,$k3,$k4);
           sort($keys);
           $keys = implode('',$keys);
           if (array_key_exists($keys,$results))
            continue;
           $results[$keys] = "($k1) $v1 + ($k2) $v2 + ($k3) $v3 + ($k4) $v4";
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }
}
echo "For a value of $target the answers are:<br>";
foreach ($results as $v)
   echo "$v<br>";
//

It is rather mechanical and I'm ABSOLUTELY POSITIVE that some php genius out there will come up with a shorter method, but this code will do combinations from 1 to 4 values.  I'll let you figure out how to incorporate 5, 6 and 7 values into it. 

Edited by ginerjm

The programmer in ME wouldn't let this go without a stab at it.

$nums = array("A"=>10,"B"=>20,"C"=>15,"D"=>25,"E"=>40,"F"=>50,"G"=>5);
$target = 80;
$results = array();
foreach($nums as $k1=>$v1)
{
 if ($v1 == $target)
 {
  $keys = $k1;
  $results[$keys] = $v1;
 }
 else
 {
  foreach($nums as $k2=>$v2)
  {
   if ($k2==$k1)
    continue;
   else
   {
    if ($v1+$v2 == $target)
    {
     $keys = array($k1,$k2);
     sort($keys);
     $keys = implode('',$keys);
     if (array_key_exists($keys,$results))
      continue;
     $results[$keys] = "($k1) $v1 + ($k2) $v2";
    }
    else
    {
     foreach($nums as $k3=>$v3)
     {
      if ($k3==$k1 || $k3==$k2)
       continue;
      else
      {
       if ($v1+$v2+$v3 == $target)
       {
        $keys = array($k1,$k2,$k3);
        sort($keys);
        $keys = implode('',$keys);
        if (array_key_exists($keys,$results))
         continue;
        $results[$keys] = "($k1) $v1 + ($k2) $v2 + ($k3) $v3";
       }
       else
       {
        foreach($nums as $k4=>$v4)
        {
         if ($k4==$k1 || $k4==$k2|| $k4==$k3)
          continue;
         else
         {
          if ($v1+$v2+$v3+$v4 == $target)
          {
           $keys = array($k1,$k2,$k3,$k4);
           sort($keys);
           $keys = implode('',$keys);
           if (array_key_exists($keys,$results))
            continue;
           $results[$keys] = "($k1) $v1 + ($k2) $v2 + ($k3) $v3 + ($k4) $v4";
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }
}
echo "For a value of $target the answers are:<br>";
foreach ($results as $v)
   echo "$v<br>";
//

It is rather mechanical and I'm ABSOLUTELY POSITIVE that some php genius out there will come up with a shorter method, but this code will do combinations from 1 to 4 values.  I'll let you figure out how to incorporate 5, 6 and 7 values into it. 

 

Hi ginerjm,

 

Thanks a lot for tyring to help me with the code. I does the job as mentioned in my post. I am sorry for not mentioning that the values of A to G are not just 7, they are infact 100, I mentioned from 1 to 7 as an example. You can name them anything like A, B, C or A1, B1, C1, etc.

 

I am novice at PHP and am anxiously waiting for some PHP guru to help me find the solution.

 

Once again, thank you very much for your assisntace.

Why would a new php person be interested in doing such a trivial exercise?  You learn nothing from it.  Instead learn how to handle data - including inputs, query results and outputs - properly.  It is one of the more important things to accomplish so that your future work is not prone to attack and your database is safe from damage.  A simple mathematics problem (100 values - really?) like this is understood and resolved by doing only 4 values as I did - why go further?  Unless this is a homework problem and if so, you have used me and I wash my hands.

 

And - for the future - please don't quote entire messages like that.  We can see the text in the previous posts.  Quoting is meant to single out a specific portion of a post for further discussion.

Edited by ginerjm

You learn nothing from it.  Instead learn how to handle data - including inputs, query results and outputs - properly.  It is one of the more important things to accomplish so that your future work is not prone to attack and your database is safe from damage.

 

You don't learn without asking, I tried solving the issue but wasn't able to, that is enough to post queries here for assistance and that is what this forum is all about. Please don't assist people here if you don't want to help, but if you do ............ don't lead others down.

 

This forum is all about learning php, mutual help and collaborative efforts.

I am all about helping!  I gave you the code, didn't I?   BUT - when you then say that your 'real' task is to carry this on to absurd lengths, I proposed to you that your valuable time and learning focus should be spent in better ways to help you improve as a php programmer.

Simply read and understand what I gave you and then carry it on to whatever lengths you need it to go.  Since this is a almost definitely a homework exercise,   you're probably supposed to figure out a more elegant method than my brute force one so you should probably put some effort into it.

Simply read and understand what I gave you and then carry it on to whatever lengths you need it to go.  Since this is a almost definitely a homework exercise,   you're probably supposed to figure out a more elegant method than my brute force one so you should probably put some effort into it.

 

Thanks for your reply ginerjm.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.