Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 Third_Degree: please can I have my medal! Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880477 Share on other sites More sharing options...
Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 tallberg: How practical would it be for you to use the recursive function above with a target of (for example) 10, then select 10 random entries from the returned array to give you a set of numbers that add up to 100. That would give you numbers in the range 1-9, and a minimum of 20 values adding up to 100. function recurse(&$final,&$working,$target) { if (array_sum($working) > $target) { array_pop($working); } elseif (array_sum($working) == $target) { $final[] = implode(',',$working); } else { $tmpTarget = $target - array_sum($working); for ($i = 1; $i <= $tmpTarget; $i++) { $working[] = $i; recurse($final,$working,$target); array_pop($working); } } } $final = $working = array(); recurse($final,$working,10); shuffle($final); $wrkArray = array_slice($final,0,9); $wrkString = implode(',',$wrkArray); echo $wrkString; Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880491 Share on other sites More sharing options...
Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 In above: $wrkArray = array_slice($final,0,9); should be $wrkArray = array_slice($final,0,10); Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880512 Share on other sites More sharing options...
Third_Degree Posted July 22, 2009 Share Posted July 22, 2009 http://thumbs.dreamstime.com/thumb_298/12182795953C10uW.jpg There you are Mark. Ah, the good old 633 octillion solutions problem... Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880525 Share on other sites More sharing options...
tallberg Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks alot to everyone. Ill try out all of these versions and check the results. Great thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880529 Share on other sites More sharing options...
Mark Baker Posted July 22, 2009 Share Posted July 22, 2009 http://thumbs.dreamstime.com/thumb_298/12182795953C10uW.jpg There you are Mark. Ah, the good old 633 octillion solutions problem... [tex]2^\left(\$target -1\right)[/tex] [tex]- 1[/tex] Quote Link to comment https://forums.phpfreaks.com/topic/166515-split-the-number-100-into-arrays-of-numbers-that-add-up-to-100/page/2/#findComment-880541 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.