Teaser 2488: [Birthdays]
From The Sunday Times, 30th May 2010 [link] [link]
Each of George’s and Martha’s five great-grandchildren celebrates a single-digit birthday today. “If you add their ages and reverse the digits of the total”, Martha said, “you get a two-digit number divisible by Andrew’s age and by none of the others'”. “That pattern continues”, George added. “Eliminate Andrew and the same is true of the other four, giving a number divisible by Brian’s age and none of the other three. If you eliminate Brian, the same is true for the other three with Colin’s age. Eliminating Colin, this works for the other two with David’s age”.
How old are the five children?
This puzzle was originally published with no title.
[teaser2488]
Jim Randell 1:41 pm on 2 September 2025 Permalink |
I often forget about the [[
choose()]] function in the enigma.py library (see: Tantalizer 482), but this is a good chance to use it. And this gives us a nice compact program.The ages must all be different, as the first reversed sum formed must be divisible by A, but none of the others. Hence A is different from all the others. Similarly the second reversed sum is divisible by B, but not C, D, E. Hence B is different from all the others. And so on. So we can use the [[
distinct=1]] parameter to the [[choose()]] function.It wasn’t completely clear if the “2-digit” condition was meant to apply to all the reversed sums, or just the first. I applied it to all of them, although it doesn’t change the answer if this condition is ignored completely.
This Python program runs in 62ms. (Internal runtime is 326µs).
from enigma import (irange, choose, nrev, printf) # check a set of numbers def fn(*ns): # reverse the sum r = nrev(sum(ns)) # must be a 2 digit number, divisible by just the last number return (9 < r < 100 and r % ns[-1] == 0 and all(r % n > 0 for n in ns[:-1])) # choose 5 single digit ages for (E, D, C, B, A) in choose(irange(1, 9), [None, fn, fn, fn, fn], distinct=1): printf("A={A} B={B} C={C} D={D} E={E}")Solution: The ages are: Andrew = 2; Brian = 8; Colin = 3; David = 7; E = 5.
We don’t know the name of the fifth child, but it may well start with an E.
Summing the five ages and reversing gives:
And 52 is divisible by 2, but not 8, 3, 7, 5.
With the 4 ages B – E:
And 32 is divisible by 8, but not 3, 7, 5.
With the 3 ages C – E:
And 51 is divisible by 3, but not 7, 5.
With the 2 ages D – E:
And 21 is divisible by 7, but not 5.
LikeLike
Frits 3:31 pm on 2 September 2025 Permalink |
from enigma import SubstitutedExpression def check(*s): if (rsum := int(str(sum(s))[::-1])) % s[-1] or not (9 < rsum < 100): return False return all(rsum % x for x in s[:-1]) # the alphametic puzzle p = SubstitutedExpression( [ "check(E, D)", "check(E, D, C)", "check(E, D, C, B)", "check(E, D, C, B, A)", ], answer="(A, B, C, D, E)", env=dict(check=check), d2i=dict([(1, "BCDE")]), digits=range(1, 10), verbose=0, # use 256 to see the generated code ) # print answers for ans in p.answers(): print(f"{', '.join((x + ': ' + y for x, y in zip('ABCDE', [str(a) for a in ans])))}")LikeLike
GeoffR 8:37 pm on 2 September 2025 Permalink |
from itertools import permutations for a, b, c, d, e in permutations(range(1, 10), 5): fg = a + b + c + d + e f, g = fg // 10, fg % 10 if not f > 0 and g > 0: continue gf = 10*g + f if gf % a == 0 and all (gf % x != 0 for x in (b, c, d, e)): # Ekiminate Andrew hi = b + c + d + e h, i = hi // 10, hi % 10 if not h > 0 and i > 0: continue ih = 10*i + h # Eliminate Brian if ih % b == 0 and all( ih % x != 0 for x in (c, d, e)): jk = c + d + e j, k = jk // 10, jk % 10 if not j > 0 and k > 0: continue kj = 10*k + j # Eliminate Colin if kj % c == 0 and all(kj % x != 0 for x in (d, e)): Lm = d + e L, m = Lm // 10, Lm % 10 if not L > 0 and m > 0: continue mL = 10*m + L # Eliminate David if mL % d == 0 and mL % e != 0: print(f" A = {a}, B = {b}, C = {c}, D = {d}, E = {e}") # A = 2, B = 8, C = 3, D = 7, E = 5LikeLike
Ruud 7:57 am on 4 September 2025 Permalink |
import istr def check(ages, i): return [sum(ages[i:])[::-1].is_divisible_by(age) for age in ages[i:]] == [True] + (4 - i) * [False] for ages in istr.permutations("123456789", 5): if all(check(ages, i) for i in range(4)): print(ages)LikeLike
Ruud 2:56 pm on 4 September 2025 Permalink |
As a one-liner:
import istr print( [ ages for ages in istr.permutations("123456789", 5) if all(all(sum(ages[i:])[::-1].is_divisible_by(ages[j]) == (j == i) for j in range(i, 5)) for i in range(4)) ] )LikeLike