[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
A farmer tells his son to select five watermelons to take to market. Because the watermelons are sold by weight, they must be put on a scale before the trip to town, but the son makes a small mistake and weighs them in (all possible) pairs. Here are the weights he comes up with, in pounds: 20, 22, ,23, 24, 25, 26, 27, 28, 30, 31. How much does each of the watermelons weigh? (Source: Sit & Solve® Brainteasers (Sit & Solve® Series)
Background & Techniques
To solve, we'll use the fact that the two lightest melons must have a
combined weight of 20 lbs, and the two heaviest combine for 31 lbs.
Mathematically, there are indeed exactly 10 ways to choose two objects from a
set of 10. Actually there are 20 ways (5 choices for the 1st object and, for
each of these, 4 choices for the 2nd). These are the "permutations", but if, as
in our case, the order of choosing doesn't matter, then half the pairs contain
the same objects as the other half, just chosen in reverse order. The 10 pairs
under these conditions are the "combination" results for choosing 2 of 10.
Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.
My original thought for modeling the problem was an array for each of the 5 melons and generating all combinations of 5 numbers which sum to 64 but re-examined the problem when I realized that there must be a better way. Ordering the melons by weight and knowing the combined weights of the two lightest and two heaviest leads to the exact weight of the middle melon. This simplifies the problem considerably. Limiting the range of lightest in the lightest pair (A) from 7 to 10 and the lightest in the heaviest pair (D) from 13 to 15, knowing that B is 20 - A and E is 31-D and C=13 allows us to generate all 12 possible weight sets.
Of those, at least one set must sum in pairs to the 10 given pair weights. To find which one (or ones) meet this criteria, I check each set using a 10 item Boolean array, WeightFound, initialized to False and set entry I to True if the sum of those two melon matches the target sum for entry I. If one matches a location already set to True or doesn't match any of the given pair values, then that set is rejected by setting the variable OK to False. Anything that is not rejected, is a solution!
Suggestions for Further Explorations
Copyright © 2000-2016, Gary Darby All rights reserved.