Combinations of numbers

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:

                     $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. :smiley:

Cheers!

Hi there,

This seemed to work for me:
[php]
if(isset($_POST[‘target’]) && is_numeric(trim($_POST[‘target’])))
{
$target = (integer)trim($_POST[‘target’]);
$middle = floor($target/6);
$range = range($middle-2,$middle+3);
if(array_sum($range) == $target)
{
echo implode(" + “,$range).” = ".$target;
}
else
{
echo “cant be done”;
}
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service