Teaser 2960: Bags of sweets!
From The Sunday Times, 16th June 2019 [link] [link]
I recently bought a number of equally priced bags of sweets for a bargain price, spending more than 50p in total. If they had been priced at 9p less per bag, I could have had 2 bags more for the same sum of money. In addition, if each had cost 12p less than I paid, then I could also have had an exact number of bags for the same sum of money.
How much did I spend in total on the sweets?
[teaser2960]







Jim Randell 11:47 pm on 14 June 2019 Permalink |
If we buy n bags of sweets at x pence per bag, then the total outlay n.x can be expressed as:
(for some whole number k, where n > 1).
From which we see:
This Python program finds the first value of n that satisfies the conditions and gives an integer value for k.
Run: [ @repl.it ]
from enigma import (Rational, irange, inf, div, printf) Q = Rational() # consider n the number of bags of sweets bought for n in irange(1, inf): x = Q(18 + 9 * n, 2) t = n * x if not (t > 50): continue # find the number of bags if the prices were 12p less k = div(t, x - 12) if k is None: continue printf("{t}p = {n} bags @ {x}p [= {n2} bags @ {x9}p = {k} bags @ {x12}p]", n2=n+2, x9=x-9, x12=x-12) breakSolution: The total spend was 216p.
With a bit more analysis we can show this is the only solution.
We can write an expression for k as:
And this only gives a whole number when 16 / (9n − 6) has a fractional part of 1/3.
This is only possible for n = 1, 2, 6.
n = 1 gives x = 13.5p, n.x = 13.5p, which is not more than 50p (and we want n > 1 anyway).
n = 2 gives x = 18p, n.x = 36p, which is not more than 50p.
n = 6 gives x = 36p, n.x = 216p, so this is the solution.
Here is a program that uses this analysis to consider all possible solutions, by looking at the divisors of 48:
from enigma import (Rational, divisors, div, printf) Q = Rational() # consider divisors of 48 for d in divisors(48): # we only want divisors of the form (3z + 1) if not (d % 3 == 1): continue # compute n n = div(48 // d + 6, 9) if n is None: continue # compute x and t x = Q(18 + 9 * n, 2) t = n * x if not (t > 50): continue # compute k k = div(9 * n * (n + 2), 9 * n - 6) # output solution printf("[d={d}] n={n} x={x} t={t} k={k}")Removing the check at line 15 will give all three solutions for the equations.
LikeLike
GeoffR 6:49 pm on 16 June 2019 Permalink |
We can put Jim’s initial three equations into a MinZinc constraint for an easy solution
LikeLike
Jim Randell 8:46 am on 17 June 2019 Permalink |
@GeoffR: This approach works OK for cases where the price per bag is a whole number (which is the case in the actual solution).
I didn’t assume that and found there were 3 candidate solutions that satisfied the 2 equations, one of which has the bags priced with a fractional amount. Two of the candidates are eliminated by the inequality (including the one where the bags are priced a fractional amount), leaving a single solution.
LikeLike
GeoffR 3:12 pm on 17 June 2019 Permalink |
@Jim: I can see you are making a mathematical point about prices for fractional amounts, but is this applicable for this teaser ?
We don’t have fractional pennies these days in our monetary system, so maybe we should assume that prices per bag are in whole numbers of pennies ?
LikeLike
Jim Randell 4:56 pm on 18 June 2019 Permalink |
@GeoffR: It was just a comment to try and extract a bit more interest from a relatively straightforward puzzle.
I try not to make additional assumptions about the puzzle if I can help it. From the formula for x we see that the price per pack is a whole number of half-pennies, so it seemed reasonable to allow this. And we do get an extra potential solution if we do. Although this solution is then removed by the “more than 50p” requirement, so it doesn’t really matter if we consider it or not.
LikeLike