Sunday Times Teaser 2977 – Enjoying Retirement
by Danny Roth
Published October 13 2019 (link)
George and Martha have worked on separate departments of a company which has fourdigit telephone extensions. George looked at his extension and it was abcd. Martha’s (larger) also had a, b and c as her first three digits but not necessarily in that order. Her last digit was e. They added up their two fourdigit numbers and found that the least significant digit was f. They then looked at the difference and that was a fourdigit number of which the least significant digit was g. They then looked at the product and the least significant digit was h. They then looked at the average of the extensions; in it the first two digits were equal, the last two digits were also equal, and the least significant digit was i. I have thus mentioned nine digits, all positive and unequal.
What was Martha’s extension?

GeoffR permalink123456789101112131415161718192021222324252627282930313233343536from itertools import permutationsmlist = []for p in permutations('123456789'):a, b, c, d, e, f, g, h, i = p# find George's extension (abcd)george = int(a + b + c + d)# find Martha's extension whose first three# digits are (abc) in some orderma, mb, mc, me = int(a), int(b), int(c), int(e)ma1 = 1000 * ma + 100 * mb + 10 * mc + mema2 = 1000 * ma + 100 * mc + 10 * mb + mema3 = 1000 * mb + 100 * mc + 10 * ma + mema4 = 1000 * mb + 100 * ma + 10 * mc + mema5 = 1000 * mc + 100 * mb + 10 * ma + mema6 = 1000 * mc + 100 * ma + 10 * mb + memlist = [ma1, ma2, ma3, ma4, ma5, ma6]# check other teaser constraintsfor martha in mlist:if martha > george:if (martha + george) % 10 == int(f):if (martha  george) % 10 == int(g):if (martha * george) % 10 == int(h):# find average of George and Martha's extensionav, r = divmod(martha + george, 2)# first two integers of average are the same digitif not r and av // 1000 == av // 100 % 10:# last two integers of average are the same digit (i)if av // 10 % 10 == av % 10 == int(i):print(f"George's Extn. is {george}")print(f"Martha's Extn. is {martha}")

GeoffR permalink123456789101112131415161718192021222324252627from itertools import permutations# George's number  abcdfor G in range(1234, 9877):# Martha's number  permute abcfor p in permutations(str(G)[:3]):# and add last digit efor x in range(1, 10):M = 10 * int(''.join(p)) + xif M > G:s5 = {G // 1000, G // 100 % 10, G // 10 % 10, G % 10, M % 10}if len(s5) == 5 and 0 not in s5:# three conditions  add, subtract and multiply G and Ms8 = s5  {(G + M) % 10, (G  M) % 10, (G * M) % 10}if len(s8) == 8 and 0 not in s8:# find average of G and Mav = (G + M) // 2if av // 1000 == av // 100 % 10:if av // 10 % 10 == av % 10:s9 = s8  {av % 10}if len(s9) == 9 and av % 10:print(f"George = {G}, Martha = {M}, average = {av}")


Erling Torkildsen permalink1234567891011121314151617181920212223# Build a set of all meanvalues (M+G)/2 on the form 'wwii'm = set()for q in range(1, 10):for p in range(1, 10):if p != q:m.add(11 * (100 * p + q))# Compute Ms and Gs from their meanfor u in m:for v in range(1, 5000):M, G = u + v, u  vif G > 999 and M < 9877 and M  G > 999:(a, b, c, d), (x, y, z, e) = str(G), str(M)f, g, h, i = (str(x % 10) for x in (M + G, M  G, M * G, u))# Tests to be satisfiedt1 = 4 == len({a, b, c, d}) == len({x, y, z, e})t2 = sorted((a, b, c)) == sorted((x, y, z))t3 = len({a, b, c, d, e, f, g, h, i}) == 9if all((t1, t2, t3)):print(f"M = {M}, (G = {G})")