Sunday Times Teaser 2795 – No Mean Feat
by Nick MacKinnon
Published: 17 April 2016 (link)
I bought a run of consecutively-numbered raffle tickets, some of which were red and the rest were blue. Amongst my numbers no red number was the average of two other reds, and no blue number was the average of two other blues. I actually won the raffle. My two-figure winning red number was in fact the geometric mean of two other red numbers of mine. [The geometric mean of M and N is the square root of MxN.]
What were my blue numbers?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from itertools import combinations# Generate sequences of consecutively numbered tickets,# coloured either red or blue, in which no red (blue)# ticket has a number that is the average of the numbers# on two other red (blue) tickets. For the averages we# can number tickets starting at zero since a constant# added to each ticket number will make no difference.def solve(seq):ln = len(seq)# we need at least 4 tickets (3 red and 1 blue)if ln > 3:yield seq# try to add a (red or blue) ticket at position 'ln'for c in 'RB':# where possible, average its position with those of# all tickets of the same colour in the sequence, only# accepting it if all such averages that are in the# sequence are of the opposite colourif all(seq[(i + ln) // 2] != c for i, v inenumerate(seq) if v == c and not (i + ln) % 2):yield from solve(seq + c)# for three increasing ticket positions i, j and k in the# sequence and a starting ticket number n, the geometric# mean constraint requires that:## (n + i) * (n + k) = (n + j) ** 2## Solving for n knowing i, j and k gives:## n = (j * j - i * k) / (i + k - 2 * j)# find valid sequences of red/blue ticketsfor s in solve(''):# find three positions of red tickets that match the# geometric mean constraintir = [i for i, v in enumerate(s) if v == 'R']for i, j, k in combinations(ir, 3):n, r = divmod(j * j - i * k, i + k - 2 * j)# the winning ticket has two digitsif not r and n > 0 and n + j > 9:# find the numbers on the blue ticketsib = [n + i for i, v in enumerate(s) if v == 'B']bs = ' and '.join(str(b) for b in ib)bs = bs.replace(' and ', ', ', len(ib) - 2)fs = 'Blue tickets {} (winner {}, sequence {}).'print(fs.format(bs, n + j, s))