8 ms·
This is just a linear programming problem. One-liner solution in Mathematica, just for kicks: In[1]:= Solve[ Rationalize[2.15 a + 2.75 b + 3.35 c +
by illicium 13y ago
This is just a linear programming problem. One-liner solution in Mathematica, just for kicks:
In[1]:= Solve[
Rationalize[2.15 a + 2.75 b + 3.35 c + 3.55 d + 4.20 e + 5.80 f == 15.05] &&
a >= 0 && b >= 0 && c >= 0 && d >= 0 && e >= 0 && f >= 0,
{a, b, c, d, e, f}, Integers
]
Out[1]= {{a -> 1, b -> 0, c -> 0, d -> 2, e -> 0, f -> 1},
{a -> 7, b -> 0, c -> 0, d -> 0, e -> 0, f -> 0}}
- davmre 13y agoLinear programming as it's usually defined allows for the solution to involve arbitrary real numbers, and it's that flexibility that allows general LPs to be solved in polynomial time. The restriction to integers turns this into an integer programming problem, which is NP-Complete. Though that doesn't seem to bother Mathematica (which presumably has a similar sort of constraint solver working under the hood). :-)