On 24 Sep 2006, at 10:41 , Penthexquadium wrote:
On Sun, 24 Sep 2006 19:06:11 +0300, "Ahmad Al-Twaijiry"
<ahmadt@xxxxxxxxx> wrote:
I have array of numbers and I want to get out of it a list of numbers
that if I sum them it will be 100, here is my list (for example ) :
I think you can try to sort the array in reverse order, and then
calculate the sum of numbers in loops (end loop when the sum is larger
than target sum).
That seems like a very slow way to do it, I think.
First thing, sort the array, then scan the array backwards for the
first number under the target sum (in this case, 50). Then look for
another number that will add to make the target sum. So, if you find
a 90, look for a 10. If you find it, you're done. If you don't find
it, then search for the next smallest number (33) and add it. Then
repeat. If you don't find a match at 83, then add the next smallest
number, 20. But that puts you over your target, so you discard 33
and start over with 50 and the next lowest number, 20. Now you are
only looking for numbers <=20.
This might be the perfect place to use a recursive function, as long
as you are careful to limit it's iteration cycles.
No way is this going to be done quickly.
--
And, while it was regarded as pretty good evidence of criminality to
be living in a slum, for some reason owning a whole street of them
merely got you invited to the very best social occasions.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php