Sunday Times Teaser 2701 – Flipping Ages!
by Robin Nayler
The ages of ten people in my family form an arithmetic progression. My age has the same digits as that of my wife but in reverse order. This is also true for my mother and my sister, my daughter and her daughter, and my son and his son. Also the product of my brother’s age and that of his son is equal to the year of birth of one of the ten people.
What are the ages of my brother, my sister and myself (in that order)?
One Comment
Leave one →
-
brian gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from operator import itemgetter# for formatting the outputfs = 'My brother is {}, my sister is {}, and I am {}.'# the current yearnow = 2014# for two digit numbers to be the reverse of each other# their difference must be a multiple of nine, which# is hence the increment in the arithmetic progressionfor a in range(1, 37):# we need ten numbers for the agesages = tuple(a + 9 * i for i in range(10))# find four reversible pairs and the leftovers# (i.e. the ages of the brother and his son)pairs, leftovers = [], []for n in ages:r = 10 * n if n < 10 else int(str(n)[::-1])if r != n and r in ages:if n < r:pairs.append((n, r))else:leftovers.append(n)if len(pairs) == 4 and len(leftovers) == 2:# sort the pairs in order of their oldest memberspairs = tuple(sorted(pairs, key=itemgetter(1)))(son, brother) = leftovers# the product of the brother's and his son's age# must give a sensible year of birth with an age# now that matches one of our agesage = now - son * brotherif age in ages or age - 1 in ages:# considering the older members of each pair, the# son and the daughter are the lowest, myself or# my wife are next and my mother is last - check# that the son's and daughter's ages are not too# far apartif pairs[1][1] - pairs[0][1] < 50:# the sister is paired with the mother and my# wife and I are the third pairsister, mum = pairs[3]us = pairs[2]# ensure my age relative to my mum is sensibleme, = tuple(x for x in us if 12 < mum - x < 50)print(fs.format(brother, sister, me))