3 ms·
In this case, since the possibilities are fairly small, you can solve this pretty quickly anyways. I wrote a very naive algorithm in ruby and it solved it it in
by Aaronneyer 13y ago
In this case, since the possibilities are fairly small, you can solve this pretty quickly anyways.
I wrote a very naive algorithm in ruby and it solved it it in 7 seconds.
# Initialize it with the most we can need of any individual numbers, plus the maximum possible zero's we could use
# (Just picked 5, because it can't take more than 7, and we have at least 2 of everything. I know there are better ways of doing this, but I was just hacking something up quickly).
x = [2.15,2.75,3.35,3.55,4.2,5.8].map{|n| [n]*(15.05/n).to_i}.flatten + [0]*5
# Find all of the combinations that add up to 15.05
x.combination(7).select{|c| c.inject(:+)==15.05}.uniq
There are two answers to this.
[2.15,2.15,2.15,2.15,2.15,2.15,2.15]
[2.15,3.55,3.55,5.80]