spfoonnewb Posted April 9, 2007 Share Posted April 9, 2007 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 https://forums.phpfreaks.com/topic/46327-number-combinations/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.