Sunday Times Teaser 2566 – A Fulfilling Strategy
by John Owen
Published: 27 November 2011 (link)
I drove down a road with a number of petrol stations whose locations I saw on my map. I decided to check the price at the first station then fill up when I found one offering a lower price (or, failing that, the last one). Looking back, I see each price per litre was different. Overall they were consecutive whole numbers of pence plus 0.9p in each case (ie 130.9p, 131.9p, 132.9p etc, not necessarily in that order).
So I’m surprised to discover the average price was a whole number of pence per litre [1].
How many petrol stations were there?
[1] This sentence is as published but it was not what John Owen intended. His correct version was wrongly edited by a sub-editor at the Sunday Times in a way that changed its meaning. John Owen has kindly provided the correct sentence as “Therefore, I am surprised to discover that the average price I could have expected to pay using my strategy was a whole number of pence per litre”.
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334from itertools import count, permutations# Since we are considering average prices, adding a constant value to# all prices will just add this constant to the average; so we can# consider the integers to be 0 .. N - 1, for N petrol stations. Use# 0.1p units to keep all costs in integers# the number of petrol stations visitedfor N in count(1):# accumulate the costs and the visit countsacc, count = 0, 0# consider all possible orders in which we can# visit the N petrol stations:for p in permutations(range(N)):# look for a station that is cheaper than the# first in this order of visitsfor i in p:if i < p[0]:# add it to the accumulator for the averageacc += 10 * i + 9count += 1breakelse:# we didn't find a cheaper one - use the last oneacc += 10 * p[-1] + 9count += 1# now compute the average and look for one that# is an integeraverage, rem = divmod(acc, 10 * count)if rem == 0:print('There were {} petrol stations.'.format(N))break