Jump to content

number combinations


spfoonnewb

Recommended Posts

Hi,

 

I have created this script to generate number combinations for variables given, with the amount of variables selected..

 

So say you choose the number 10, and 3 total variables, the following should appear:

 

5 + 2 + 3,

2 + 4 + 6

..and so on

 

I have written the script below, and it only displays combinations where any extra numbers other than the first one are the same.. I need the numbers to all be different.

 

So instead of what I want, it shows like:

 

4 + 2 + 2

8 + 1 + 1

..and so on

 

The second two (or more) numbers are always the same..

 

<?php

//Number you wish to add up to
$n = 10;

//How many total variables there is already 2 present.. so its + how ever many
$w = 1;

for ($ima = 1; $ima <= $n; ++$ima) {
    $num[] = $ima;
    foreach ($num as $v) {
        
        $nar = array('');
        
       	$str = "$ima + $v ".str_repeat("+ $v ", $w)."";
        
        if (!in_array($str, $nar)) {
            $nar[] = $str;
        }
        
        echo "<pre>".print_r($nar)."</pre>";
    }
}
?>

Link to comment
Share on other sites

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.