Teaser 2557: The legacy
From The Sunday Times, 25th September 2011 [link] [link]
In my will I have set aside a five-digit sum of pounds for charity — a group of four animal charities and a group of seven children’s charities. This five-digit sum consists of five consecutive non-zero digits, not in numerical order. On my death this legacy is to be divided equally between these charities. But I have left it to my executors’ discretion to choose to divide it instead equally among the charities in just one of the groups. Each donation will be a whole number of pounds, whichever course they take.
What is the five-digit sum?
[teaser2557]










Jim Randell 8:32 am on 11 April 2025 Permalink |
The amount must be divisibly by 4, 7, 11, and LCM(4, 7, 11) = 308.
So we are looking for a 5-digit number that is a multiple of 308, and is composed from a set of 5 different non-zero consecutive digits, but not in numerical order.
This Python program looks at 5-digit multiples of 308. It has a runtime of 63ms, and an internal runtime of 1.7ms.
from enigma import (irange, mlcm, nsplit, is_consecutive, printf) # look for 5-digit multiples of 4, 7, 11 k = mlcm(4, 7, 11) for n in irange.round(10000, 99999, step=k, rnd='I'): # split into digits ds = nsplit(n) # look for consecutive non-zero digits if (0 in ds) or is_consecutive(ds) or not is_consecutive(sorted(ds)): continue # output solution printf("amount = {n}")But it is slightly faster (but also slightly longer) to start with the possible non-zero consecutive digits, and then consider rearrangements of those digits.
This Python program also runs in 63ms, but has an internal runtime of 784µs.
from enigma import (irange, mlcm, tuples, subsets, nconcat, is_consecutive, printf) # amount must be divisible by 4, 7, 11 k = mlcm(4, 7, 11) # choose 5 consecutive digits for ds in tuples(irange(1, 9), 5): # consider possible numbers formed from these digits for ds in subsets(ds, size=len, select='P'): n = nconcat(ds) # check divisibility if not (n % k == 0): continue # check the digits are not consecutive if is_consecutive(ds): continue # output solution printf("amount = {n}")Solution: The amount is: £ 74536.
If a 0 digit is permitted there is a second solution of £ 43120.
LikeLike
Frits 4:59 pm on 11 April 2025 Permalink |
Using divisibility rules.
from itertools import permutations # suppose d1 + d3 + d5 = d2 + d4 + k # then k % 11 = 0 as legacy is divisible by 11 (alternating sum) # t = sum(d1...d5) = 2 * (d2 + d4) + k for i, t in enumerate(range(15, 36, 5), 1): # it t odd then k = 11, it t even then k = 0 k = 11 if t % 2 else 0 d2d4 = (t - k) // 2 if d2d4 < 2 * i + 1 or d2d4 > 2 * i + 7: continue for d2 in range(i, i + 5): d4 = d2d4 - d2 if d4 == d2 or d4 not in range(i, i + 5): continue for d1, d3, d5 in permutations(set(range(i, i + 5)) - {d2, d4}): if d5 % 2: continue n45 = 10 * d4 + d5 # divisibility by 4 if n45 % 4: continue # divisibility by 7 rule (alternating sum of blocks of three) if (100 * d3 + n45 - 10 * d1 - d2) % 7: continue print("answer:", ''.join(str(x) for x in (d1, d2, d3, d4, d5)))LikeLike