Teaser 2777: Summing up 2015
From The Sunday Times, 13th December 2015 [link] [link]
I asked Harry and Tom to write down three numbers that between them used nine different digits and which added to 2015. They each succeeded and one of Harry’s three numbers was the same as one of Tom’s. I noticed that Harry’s three numbers included a perfect square and Tom’s included a higher perfect square.
What were those two squares?
[teaser2777]
Jim Randell 8:44 am on 23 February 2021 Permalink |
As stated there are multiple solutions to the puzzle.
Having tackled similar problems before it seems likely that the intention of the setter is:
or possibly:
Any solution for the latter will appear as a solution for the former, so we will use the first one to solve the puzzle (as it turns out it does give a unique answer).
The following Python code generates possible sums that use a square and 2 other numbers to produce the required total, and where each of 9 digits is used exactly once in the summands.
It then choose sums that use different squares, but have one of the other numbers in common.
It runs in 95ms.
Run: [ @repl.it ]
from enigma import ( cproduct, powers, inf, is_duplicate, irange, concat, multiset, group, unpack, subsets, intersect, join, arg, printf ) # target sum Y = arg(2015, 0, int) printf("[Y={Y}]") # generate (s, a, b) sums, s is square, s + a + b = Y def generate(Y): # consider squares for s in powers(0, inf, 2): if s > Y: break if is_duplicate(s): continue # find sums: s + a + b = Y t = Y - s for a in irange(0, t // 2): b = t - a # check for sums with 9 different digits ds = concat(s, a, b) # check for 9 different digits if len(ds) == 9 and len(set(ds)) == 9: yield (s, a, b) # group sums by the square d = group(generate(Y), by=unpack(lambda s, a, b: s)) # collect the squares involved r = multiset() # consider squares for Harry and Tom for (sH, sT) in subsets(sorted(d.keys()), size=2): # choose decompositions for (dH, dT) in cproduct([d[sH], d[sT]]): # that share a number if intersect([dH, dT]): printf("[H = {dH}; T = {dT}]", dH=join(dH, sep=" + "), dT=join(dT, sep=" + ")) r.add((sH, sT)) # output solutions for (k, v) in r.most_common(): printf("squares = {k} [{v} solutions]", k=join(k, sep=", "))Solution: Harry’s square was 324. Tom’s square was 784.
There are two ways to arrive at the solution:
The summands in each sum use all the digits exactly once except for the digit 1.
To see the multiple solutions without the restriction that each of the 9 digits is used exactly once, you can remove line 15 and the [[
len(ds) == 9]] clause from line 24.The same puzzle could have been set in 2019, and had the same answer.
If the puzzle were set in 2022, there would only be one way to arrive at the (different) answer.
LikeLike
Frits 2:04 pm on 23 February 2021 Permalink |
I assumed Harry’s and Tom’s squares were not the number they shared (ABCD).
from enigma import SubstitutedExpression, is_square # the alphametic puzzle p = SubstitutedExpression( [ # Use 4-digit numbers as sum is limited to 2015 # to reduce the output assume EFGH (Harry) and MNOP (Tom) # are both perfect squares "is_square(EFGH)", # use a variable as right hand side to reduce the number of loops "2015 - ABCD - EFGH = IJKL", # nine different digits "len(str(ABCD) + str(EFGH) + str(IJKL)) == \ len(set(str(ABCD) + str(EFGH) + str(IJKL))) == 9", # Tom's included a higher perfect square "MNOP > EFGH and is_square(MNOP)", # use a variable as right hand side to reduce the number of loops "2015 - ABCD - MNOP = QRST", # nine different digits "len(str(ABCD) + str(MNOP) + str(QRST)) == \ len(set(str(ABCD) + str(MNOP) + str(QRST))) == 9", ], answer="EFGH, MNOP", d2i=dict([(k, "AEIMQ") for k in range(3, 10)]), verbose=0, distinct="", ) # Print answers sols = set(sol for (_, sol) in p.solve()) print(f"answer = {sols}")LikeLike