All combinations of numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ematr1x
Forum Newbie
Posts: 1
Joined: Sat Feb 26, 2011 12:14 pm

All combinations of numbers

Post by ematr1x »

Hey guys, I have been trying to figure this out for a bit and it's really been stumping me.

I am trying to find every combination of 6 numbers that = x.

All numbers must be between 1 and 49 so the smallest number would be 21 (1 + 2 + 3 + 4 + 5 + 6) and the largest 279 (44 + 45 + 46 + 47 + 48 + 49)

Example:

Every combination of numbers a + b + c + d + e + f that equals 150

I'm thinking I can do it with a dozen do while loops

Example:

Code: Select all

                     $one = 1;
                     $two = 2;
                     $three = 3;
                     $four = 4;
                     $five = 5;
                     $six = 6;
                     //$num = 1;
                     
                     do {
                        $total = $one + $two + $three + $four + $five + $six;
                        $set = 1;
                        echo "<tr>";
                           echo "<td>" . $one . "</td>";
                           echo "<td>" . $two. "</td>";
                           echo "<td>" . $three . "</td>";
                           echo "<td>" . $four . "</td>";
                           echo "<td>" . $five . "</td>";
                           echo "<td>" . $six . "</td>";
                           echo "<td>" . $total . "</td>";
                           echo "<td>" . $set . "</td>";
                        echo "</tr>";
                        $six++;
                       
                        $num++;
                     }while($six < 49);
                     
                     do {
                        $total = $one + $two + $three + $four + $five + $six;
                        $set = 2;
                        echo "<tr>";
                           echo "<td>" . $one . "</td>";
                           echo "<td>" . $two. "</td>";
                           echo "<td>" . $three . "</td>";
                           echo "<td>" . $four . "</td>";
                           echo "<td>" . $five . "</td>";
                           echo "<td>" . $six . "</td>";
                           echo "<td>" . $total . "</td>";
                           echo "<td>" . $set . "</td>";
                        echo "</tr>";
                        $five++;
                       
                        $num++;
                     }while($five < 48);
Etc, etc but there has to be a simpler way.

Thanks for any kind of help. :D

Cheers!
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: All combinations of numbers

Post by califdon »

http://en.wikipedia.org/wiki/Category:C ... algorithms
Use a search engine with terms like combinatorial algorithm
Post Reply