Car Talk - Reversed Age Puzzler

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

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.

Mensa® Daily Puzzlers

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.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(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

 

 

 

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;

Running/Exploring the Program 

bullet Download source
bullet Download  executable
Original:  April 1, 2008

Modified:  May 12, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.