Programmatic / mathematics problem
Posted: Thu Dec 10, 2009 1:25 am
Okay, this is a real-life scenario for the software my team and I are developing at work. Nope, it's not a homework assignment.
Let's say I have a total: 21.30
I need to take the numbers out of this list:
10.03, 9.50, 7.10, 7.10, 7.10, 5.20, 4.11
To get to the closest number that matches the total (21.30).
For example:
10.03 + 9.50 = 19.53
7.10 + 7.10 + 7.10 = 21.30 (wins)
The total and the numbers in the list are always dynamic.
We have already thought of starting with the lowest number and looping through the calculations from lowest to highest, rotating the lowest number as we go, until we hit the closest possible match. This may work, but we think there has got to be a better solution.
Our lead developer has a BA in mathematics and he is scratching his head on this one.
Any ideas?
Let's say I have a total: 21.30
I need to take the numbers out of this list:
10.03, 9.50, 7.10, 7.10, 7.10, 5.20, 4.11
To get to the closest number that matches the total (21.30).
For example:
10.03 + 9.50 = 19.53
7.10 + 7.10 + 7.10 = 21.30 (wins)
The total and the numbers in the list are always dynamic.
We have already thought of starting with the lowest number and looping through the calculations from lowest to highest, rotating the lowest number as we go, until we hit the closest possible match. This may work, but we think there has got to be a better solution.
Our lead developer has a BA in mathematics and he is scratching his head on this one.
Any ideas?