Sunday Times Teaser 2948 – A Hardy Annual
by Victor Bryant
Published March 24 2019 (link)
Yesterday was my grandson’s birthday and we continued a family tradition. I asked him to use any eight different nonzero digits (once each) to form a set of numbers that added to 2019. Last year I asked the equivalent question with a sum of 2018, and I have done this each year for over ten years. Only on one occasion has he been unable to complete the task.
In this year’s answer his set of numbers included a 3figure prime that had also featured in last year’s numbers.
(a) In which year was he unable to complete the task?
(b) What was the 3figure prime that featured in this year’s answer?
5 Comments
Leave one →

Erling Torkildsen permalink12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061from itertools import permutationssums = list() # An evil global variable! Didn't make it without..def possible(): # Routine for checking possible yearsyrs = set(range(2009, 2018))s1 = set(range(2, 10))for n in range(2, 10):s2 = s1.difference((n,))for b, c, d in permutations(s2, 3):s3 = s2.difference((1, b, c, d))for e, f, g in permutations(s3, 3):s4 = s2.difference((1, b, c, d, e, f, g))for h in s4:t = 1000 + 100 * b + 10 * c + d + 100 * e + 10 * f + g + hif t in yrs:yrs.discard(t)return yrsdef get_p(): # Be selfsufficient when it comes to primesp = set()for x in range(101, 1000, 2):pr = Truefor div in range(2, 33):if x % div == 0:pr = Falsebreakif pr:p.add(x)return p# the set of three digit primesprms = get_p()def p_year(year):prime = set() # For collecting primes for returns1 = set(range(2, 10))for n in range(2, 10):s2 = s1.difference((n,))for b, c, d in permutations(s2, 3):s3 = s2.difference((1, b, c, d))for e, f, g in permutations(s3, 3):s4 = s2.difference((1, b, c, d, e, f, g))for h in s4:u = 1000 + 100 * b + 10 * c + dv = 100 * e + 10 * f + gif u + v + h == year and v in prms:prime.add(f"{v}")sums.append(f"{u} + {v} + {h} = {year}")return primefor y in possible():print(f"Not possible in {y}\n")for cp in p_year(2018).intersection(p_year(2019)):print(f"Common prime {cp} in:")for s in sums:if '+ ' + str(cp) in s:print(" ", s)

GeoffR permalink12345678910111213141516171819202122232425262728293031323334353637383940414243444546from itertools import permutationsdef is_prime(n):for x in range(2, int(n**0.5) + 1):if n % x == 0:return Falsereturn Truea = 1s2018, s2019 = set(), set()all_years= {2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019}years_ok = set()digits = set(range(2,10))for p in permutations(digits, 3):b, c, d = pabcd = 1000*a + 100*b + 10*c + ds2 = set(digits.difference([b,c,d]))for q in permutations(s2,4):e, f, g, h = qefg = 100*e + 10*f + gif not is_prime(efg): continueyear = abcd + efg + hif year == 2018:s2018.add(efg)if year == 2019:s2019.add(efg)if 2009 <= year <= 2019:years_ok.add(year)# Find a prime number common to years 2018 and 2019z, = s2018.intersection(s2019)print('Three figure prime number common to years 2018 and 2019 =', z)zz, = all_years.difference(years_ok)print('Year unable to complete task is',zz)