FXG Posted March 14, 2014 Share Posted March 14, 2014 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 14, 2014 Share Posted March 14, 2014 The trick would be to use 3 nested loops, checking that you are not summing like indices as you go. Have fun! (It's a typical programmer's task) Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 14, 2014 Share Posted March 14, 2014 oops - just realized that you are allowing for more than 3 elements in a combo. Have to do as many loops as there are individual nums and check the sum in each loop. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 14, 2014 Share Posted March 14, 2014 (edited) 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 March 14, 2014 by ginerjm Quote Link to comment Share on other sites More sharing options...
FXG Posted March 14, 2014 Author Share Posted March 14, 2014 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 14, 2014 Share Posted March 14, 2014 (edited) 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 March 14, 2014 by ginerjm Quote Link to comment Share on other sites More sharing options...
FXG Posted March 18, 2014 Author Share Posted March 18, 2014 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 18, 2014 Share Posted March 18, 2014 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. Quote Link to comment Share on other sites More sharing options...
FXG Posted March 21, 2014 Author Share Posted March 21, 2014 Can anyone please help me find out the solution to the above mentioned situation, if possible, thanks. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 21, 2014 Share Posted March 21, 2014 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. Quote Link to comment Share on other sites More sharing options...
FXG Posted March 21, 2014 Author Share Posted March 21, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.