Sunday Times Teaser 3035 – Friend of the Devil
by Stephen Hogg
Published Sunday November 22 2020 (link)
My friend, “Skeleton” Rose, rambled on with me and my uncle (“The Devil” and “Candyman”) about Mr Charlie, who gave, between us, three identical boxes of rainbow drops.
Each identical box’s card template had a white, regular convex polygonal base section with under ten sides, from each of which a similar black triangular star point extended. All these dark star points folded up to an apex, making an enclosed box.
The number of sweets per box equalled the singlefigure sum of its own digits times the sum of the star points and the box’s faces and edges. If I told you how many of the “star point”, “face” and “edge” numbers were exactly divisible by the digit sum, you would know this number of sweets.
How many sweets were there in total?

Brian Gladman permalink1234567891011121314151617181920212223from collections import defaultdictc2n = defaultdict(list)# the number of star points (s) (faces = s + 1, edges = 2.s)for s in range(3, 10):# the digit sum of ...for ds in range(1, 10):# ... the number of sweets per boxsw = ds * (4 * s + 1)# check that the digit sum is correctif sum(int(x) for x in str(sw)) == ds:# store the number of sweets per box indexed on how# many of the numbers: (star points, faces, edges)# are multiples of its digit sumcnt = sum(x % ds == 0 for x in (s, s + 1, 2 * s))c2n[cnt].append(sw)# find a number of sweets that is uniquefor k, v in c2n.items():if len(v) == 1:print(f"{3 * v[0]} sweets in total.")

John Z permalink123456789101112131415161718192021222324252627282930313233# function to return sum of digits for up to a three digit number# max value encountered will be 9 * ( 4 * 9 + 1) or 333sod3 = lambda x: sum(((a := divmod(x, 100))[0], *divmod(a[1], 10)))sols = [0] * 4 # number exactly divisiblesolns = [0] * 4 # number of sweets when exactly divisible# index over number of sides in polygonfor i in range(3, 10):faces = i + 1starpoints = iedges = i * 2# index over possible sums of digits of sweets in boxfor n in range(1, 10):sweets = n * (4 * i + 1) # i.e. n * (Faces + starpoints + edges)# test 'number of sweets per box equalled the singlefigure sum of its own# digits ...'if n == (sod := sod3(sweets)):p = (faces % sod == 0) + (starpoints % sod == 0) + (edges % sod == 0)sols[p] += 1 # 'number exactly divisble'# save number of sweets; ok to overwrite# as overwrite only occurs for nonunique solutionssolns[p] = sweets# print(i, n, sweets) # for debugging# unique solution when "number of exactly divisible" occurs once# there are 3 boxes so multiply by threeprint("Total number of sweets in the three boxes:", solns[sols.index(1)] * 3)