Search

Search
WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release
of Delphi (Delphi
10.1 Berlin Starter Edition ). There
are a few restrictions, but it is a welcome step toward making
more programmers aware of the joys of Delphi. They do say
"Offer may be withdrawn at any time", so don't delay if you want
to check it out. Please use the
feedback
link to let me know if the link stops working.

Support DFF - Shop

If you shop at Amazon anyway, consider
using this link.

We receive a few cents from each
purchase. Thanks

Support DFF - Donate

If you benefit from the website, in terms of
knowledge, entertainment value, or something otherwise useful,
consider making a donation via PayPal to help defray the
costs. (No PayPal account necessary to donate via credit
card.) Transaction is secure.

For over 15 years
Mensa Page-A-Day calendars have provided several puzzles a year
for my programming pleasure. Coding "solvers" is most fun,
but many programs also allow user solving, convenient for "fill
in the blanks" type. Below are Amazon links to the
two most recent years.

(Hint: If you can
wait, current year calendars are usually on sale in January.)

Contact

Feedback:Send an
e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

Problem Description

A Puzzle:

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.

If we label the melons A,B,C,D,E by increasing weight, then combined weights
reflect the 10 results from weighing A+B, A+C, A+D, A+E, B+C, B+D, B+E, C+D, C+E,
and D+E. Under our naming convention, we now know that A+B must = 20 lbs and D +
E must = 31 lbs. We also know that the sum of all the weighings (256 lbs)
contains each of the melons weighed 4 times, so the total weight of the melons
weighed one time is 64 lbs and therefore the weight of melon C must be 64 - 20 -
31 = 13 lbs.

By assigning letter names in increasing weight order, we also have additional
constraints on the other melon weights: B must be at most 13 lbs and D must be
at least 13 pounds.

With this background, we can enumerate the possible individual weights for pairs
AB and DE and then test each candidate set of weight to find the one (or more)
sets that can provide the 10 given pair weights.

You can work out the solution for yourself in less than an hour or click the
buttons on the program page to let it do the trial and error grunt work in less
than a millisecond!!

Programmer's Notes:

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!