Originally posted by: piroroadkill
M4H's solution may be fairly elegant, but there's a better solution to this class of problem.
3.9A + 5.9B = 212
---------------------
The first thing to do is write down the other implied equations.
A >= 0
B >= 0
Then you need to find a simple linear combination of the pipe lengths that gives a whole number (preferably one that is a factor of 212). In this case the person setting the question is kind, there's an easy linear combination:
5.9 - 3.9 = 2 (1)
Now we should really use some discrete maths, or a modified version of Pascal's theorem, or something like that to solve it. But I can't be bothered with that for such a simple question and I want a proof that people can understand. We use a bastardised induction theorem and invariants.
Look at the equation marked (1). If we take any combination of pipes that gives an even number, and add or subtract the left-hand side (LHS) of (1) enough times, we *have* to be able to get 212. We could generate any number of answers now, but we'll need to be clever to avoid negative values of A or B.
What we will do is take one of the lengths of pipe, and find a number to multiply it with that gives an *even* number that's close to 212.
I happen to want one that's higher than 212, so I'll need to use 5.9 length pipes to do it. Why? Because if I am above 212, I need to subtract the LHS of (1), and that will give me a negative number of 5.9 length pipes straight away if I had started with a number of 3.9 pipes and 0 of the 5.9 pipes. If I start with 5.9 pipes and a starting total above 212, the only way I can get a negative number of pipes after repeated subtractions of the LHS of (1) is if there are no solutions to the problem at all.
212 / 5.9 = 35.9322033898......
The nearest number above this that will give a whole answer is obviously 40. So:
40 * 5.9 = 236
And how far above are we?
236 - 212 = 24
So we need to apply equation (1) 12 times to get the right answer. But we won't bother doing this, since we now have the answer. Applying the equation twelve times will give 12 pipes of length 3.9 and 28 (40 minus 12) pipes of length 5.9, which is the answer. Yay.
The technique really works; try it with other values that aren't 212 (but keep the values above 78 if you want there to be a (guaranteed I think?) solution
Also try different pipe length values if you dare, as long as you can reliably find a simple-ish linear combination of the two pipe lengths similar to equation (1), the technique should work.