Teaser 2428: [Bags of sweets]
From The Sunday Times, 5th April 2009 [link]
For years, I have been into our local sweetshop for bags of my favourite sweets. I always buy as many bags as possible for less than a fiver.
On each occasion, had I bought one more bag, then the total cost in pence would have been like the amount I actually paid, but with the digits in a different order. The cost of a bag of my favourite sweets has gone up three times during this period.
What have been the four prices of a bag of my sweets?
“A fiver” is £ 5.
This puzzle was originally published with no title.
[teaser2424]
Jim Randell 3:11 pm on 19 June 2026 Permalink |
This Python program runs in 66ms. (Internal runtime is 2.0s).
from enigma import (irange, divf, nsplit, printf) # consider possible prices for a bag for b in irange(1, 499): # number of bags for less than 500 n = divf(499, b) # total cost t = b * n # cost of n + 1 bags t1 = t + b # do <t> and <t1> contain the same digits? if not (sorted(nsplit(t)) == sorted(nsplit(t1))): continue # output solution printf("b={b} n={n}; costs {t} -> {t1}")Solution: The four prices were: 90p, 99p, 135p, 162p.
We have:
LikeLike
Frits 3:28 pm on 19 June 2026 Permalink |
You can argue that there is no valid solution (“… gone up three times …”).
LikeLike
Jim Randell 5:30 pm on 19 June 2026 Permalink |
@Frits: Would it be better if it was phrased:
?
LikeLike
Ruud 7:03 pm on 19 June 2026 Permalink |
For once without istr:
import peek for price in range(1, 500): q0 = 499 // price cost0 = q0 * price cost1 = (q0 + 1) * price if set(str(cost0)) == set(str(cost1)): peek(price, q0, cost0, cost1)LikeLike