Sunday Times Teaser 2547 – Multiple Celebration
by R Nayler
Published: 17 July 2011 (link)
Today is my birthday and the birthday of my granddaughter Imogen. My age today is a whole-number multiple of hers, and this has been true on one third of our joint anniversaries.
If we both live until I am six times her current age, then my age will be a multiple of hers on two more birthdays.
How old are we today?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930from itertools import count# for the age of the grand-daughter from 'frm' to 'to' inclusive,# work out on how many anniversaries the grandfather's age will# be a multiple of that of the granddaughterdef count_multiples(gf_age, gd_age, frm, to):m = [1 for x in range(frm, to + 1) if (gf - gd + x) % x == 0]return len(m)# consider the granddaughter's agefor gd in range(1, 31):# and the possible multiples for the grandfather's agefor gf in range(gd, 6 * gd, gd):# up to now the grandfather's age has been a multiple of# the granddaughter's on a third of their joint birthdaysif 3 * count_multiples(gf, gd, 1, gd) == gd:# in the future, until the grandfather is more than six# times the granddaughter's current age, one will be a# multiple of the other on two occasionsif count_multiples(gf, gd, gd + 1, 7 * gd - gf) == 2:print('Their ages are {} and {}.'.format(gd, gf))breakelse:continuebreak