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 four-digit 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 four-digit numbers and found that the least significant digit was f. They then looked at the difference and that was a four-digit 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?
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334353637from itertools import permutations# the nine positive digitss9 = set(range(1, 10))# the last digit for George's numberfor d in s9:# last digit for Martha's number (the same parity as George's# because their average is an integer)for e in s9.difference((d + x) % 10 for x in (1, 3, 5, 7, 9)):# form the last digits of the sum, difference and product# and check that we have four unused positive digits leftf, g, h = (d + e) % 10, (e - d) % 10, (d * e) % 10s4 = s9.difference((d, e, f, g, h))if len(s4) == 4:# permute the remaining digits for George's top three digitsfor p in permutations(s4, 3):# extract the last remaining digiti, = s4.difference(p)# George's numbergn = 1000 * p[0] + 100 * p[1] + 10 * p[2] + d# permute George's top three digits for Martha's numberfor q in permutations(p):mn = 1000 * q[0] + 100 * q[1] + 10 * q[2] + e# ... which is at least 1000 greater than George'sif mn >= gn + 1000:# the top/bottom two digits of the average are the same# and its bottom digit is the last remaining digitt2, b2 = divmod((mn + gn) // 2, 100)if t2 // 10 == t2 % 10 and b2 // 10 == b2 % 10 == i:print(f"Martha's number: {mn} (George's number: {gn}).")
-
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 mean-values (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})")