spfoonnewb Posted April 7, 2007 Share Posted April 7, 2007 Is there a way to use PHP to find out how many ways & what numbers add up to a certain number? I was thinking give it a variable, and have it add variables together... Example: Say I use 10 I want PHP to output the numbers that add up to it: I.e. 5+5, 1+9.. *Edit I used 10 not 7 lol* Link to comment https://forums.phpfreaks.com/topic/45961-php-math/ Share on other sites More sharing options...
akitchin Posted April 7, 2007 Share Posted April 7, 2007 the easiest way would be to just use two different functions: one to find its multiplicative factors, one to imply iterate through every additive combination: <?php function get_additives($number) { // setup the array of results $combos = array(); // get the halfway mark (all combos above this will be degenerate) $halfsies = floor($number / 2); // get the additive combos for up to half the number for ($i = 0; $i <= $halfsies; ++$i) { $combos[] = array($i, ($number - $i)); } // return the results return $combos; } // echo the additives of 10 $additives = get_additives(10); foreach ($additives AS $arr) { echo $arr[0].' + '.$arr[1].'<br />'; } ?> as for grabbing the factors, i'm too lazy to write one for that right now. i'm sure there's a function online somewhere to do that for you. to be honest, this isn't PHP so much as simple math written with PHP to return the results. Link to comment https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223279 Share on other sites More sharing options...
spfoonnewb Posted April 7, 2007 Author Share Posted April 7, 2007 So say the number is bigger than 2.. Is there a way to make the numbers not be limited to only 2 variables.. so it its 500 allow 250+100+100+50? Just exploring the limits of PHP :: Link to comment https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223289 Share on other sites More sharing options...
akitchin Posted April 7, 2007 Share Posted April 7, 2007 that would be far more effort and would require recursion most likely. you'd need to whittle down your selectivity for that to happen - can you imagine how many possible combinations there are for 500 if you didn't restrict the minimum additive size? EDIT: what are you trying to do? there might be a more efficient way to accomplish what you're trying to do in the long run using these factors. Link to comment https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223293 Share on other sites More sharing options...
spfoonnewb Posted April 7, 2007 Author Share Posted April 7, 2007 "can you imagine how many possible combinations there are for 500 if you didn't restrict the minimum additive size" is exactly what I wanted. It's a problem I found, and theres more to it, I just need to get the numbers to do the rest.. I hope. Anyways.. and suggestion for where to start? Link to comment https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223302 Share on other sites More sharing options...
spfoonnewb Posted April 7, 2007 Author Share Posted April 7, 2007 So I figured out how to make it use multiple numbers.. but how can I make it recursive, rather than having to add more things.. <?php $max = 10; function get_additives($number) { $combos = array(); $halfsies = floor($number / 2); for ($i = 1; $i <= $halfsies; ++$i) { for ($i2 = 1; $i2 <= $halfsies; ++$i2) { for ($i3 = 1; $i3 <= $halfsies; ++$i3) { if ($i + $i + $i2 + $i2 + $i3 + $i3 == $number) { $combos[] = array($i, $i, $i2, $i2, $i3, $i3); } } } } return $combos; } $additives = get_additives($max); foreach ($additives AS $arr) { echo "".$arr[0]." + ".$arr[1]." + ".$arr[2]." + ".$arr[3]." + ".$arr[4]." + ".$arr[5]." = $max<br />\n"; } ?> Currently outputs: 1 + 1 + 1 + 1 + 3 + 3 = 10 1 + 1 + 2 + 2 + 2 + 2 = 10 1 + 1 + 3 + 3 + 1 + 1 = 10 2 + 2 + 1 + 1 + 2 + 2 = 10 2 + 2 + 2 + 2 + 1 + 1 = 10 3 + 3 + 1 + 1 + 1 + 1 = 10 Link to comment https://forums.phpfreaks.com/topic/45961-php-math/#findComment-223597 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.