Dennis Ranke wrote:
> David G. Andersen wrote:
> 
>> Thanks for the inspiration to improve my code a little more. :-)
> 
>  > [...]
> 
> This improved solution is nicely fast. However I have found one example 
> where it doesn't find the closest solution:

I have now looked closer at this issue and have identified two bugs in
your code.

The first (and fairly trivial one) is that you don't allow divisions
that yield 1 as their result. So your code can't solve
Target = 99, Sources = 100, 5, 5
for example. (Should be 100 - (5 / 5))

The second one is the one that allows your solution to finish this
fast.. ;) In your fs function you have two loops using the variables i
and j. Inside the inner loop you sort i and j by source[i] and
source[j]. Then in the next iteration of the inner loop you are still
using the changed i, which is of course wrong. This can be resolved by
using a copy of i in the inner loop.

Regards, Dennis