I suppose that most everyone here in the States has heard of "Click and "Clack, the Tappet brothers" (a.k.a. Ray and Tom Magliozzi) . They have a weekly "Car Talk" radio show on National Public Radio which combines entertainment with occasionally useful automotive advice. One of the features each week is a "Puzzler", sometimes automotive, sometimes a word puzzle, and sometimes math or logic related. Like last week's
When you add the age of a ship and the age of its boiler, it totals 42 years. So S + B = 42. The ship is twice as old as the boiler was when the ship was as old as the boiler is now. How old are they?
That one could be solved with simple algebra and a few trials to find equations that fit the syntax. This week's seemed to deserve a programmed solution, even though it's quite a simple one (the program that is, not the puzzle):
"Recently I had a visit with my mom and we
realized that the two digits that make up my age when
reversed resulted in her age. For example, if she's 73, I'm 37. We wondered how
often this has happened over
the years but we got sidetracked with other topics and we never came up with an
answer.
"When I got home I figured out that the digits of our ages have been reversible
six times so far. I also figured
out that if we're lucky it would happen again in a few years, and if we're
really lucky it would happen one
more time after that. In other words, it would have happened 8 times over all.
So the question is, how old
am I now?"
The Delphi program which finds the solution required about 30 lines of code. It works like this: The common characteristic of the 8 pairs of Son-Mother ages we are looking for must all have the same age difference. So we'll try all the feasible age differences between Mother and Son, say from 10 to 50 years. It's probably safe to assume that the oldest feasible Mom's age is less than 100 so we only need to check son's ages up 100 minus the current difference being checked. Given those assumptions, the Delphi code below probably describes the algorithm better than text, even for non-programmers. {Comments in Delphi are enclosed in curly brackets}
for diff:=10 to 50 do
{loop on possible age differences}
begin
count:=0; {reset Nbr of reversals for this
age difference}
for son:=1 to 100-diff do {for all
reasonable son's ages}
begin
mom:=son+diff;
{"age div 10" will be the
leftmost and "age mod 10" will be the rightmost digit}
if (son mod 10 = mom div 10) and (son div 10=mom mod
10) then
begin {save the ages, in
case this is the solution case}
inc(count);
{count the reversals, "inc" means increment}
ages[count]:=point(son,mom);
{save the ages}
end;
end;
if count=8 then { a solution!}
begin
memo2.lines.add(format('Age diff:%d,
Reversals:%d',[diff,count]));
for i:=1 to count do {display
the 8 flipped age values}
with memo2, ages[i] do
if i<>6 then lines.add(format('Son:%d, Mom:%d',[x,y]))
else {flag the 6th age, the
solution}
lines.add(format('Son:%d, Mom:%d <==== Solution!',[x,y]));
end;
end;
| Original: April 1, 2008 |
Modified: November 07, 2008 |