Teaser 2540: Extra time
From The Sunday Times, 29th May 2011 [link] [link]
George and Martha planned a holiday on the south coast. The second-class rail fare each way was a certain whole number of pounds per person and the nightly cost of an inexpensive double room, in pounds, was the same number but with digits reversed. They originally planned to stay 30 nights, but then increased that to 33. “So the total cost will go up by 10%”, said Martha. “No”, replied George, “it will go up by some other whole number percentage”.
What is the nightly cost of a double room?
[teaser2540]
Jim Randell 8:11 am on 29 October 2024 Permalink |
I assumed that the rail fare does not have a trailing zero, so that the room rate does not have a leading zero.
This Python program finds the first solution where the rail fare and room rate are less than £100.
It runs in 80ms. (Internal runtime is 188µs).
from enigma import (irange, nrev, div, printf) # consider possible rail fares for F in irange(1, 99): # disallow trailing zeros if F % 10 == 0: continue # the room rate is the reverse of this R = nrev(F) # cost for 30 nights and 33 nights t30 = 4*F + 30*R t33 = 4*F + 33*R # t33 is a k per cent increase on t30 r = div(100 * t33, t30) if r is None: continue printf("F={F} R={R} r={r}%") breakSolution: The cost of the room was £ 54 per night.
And so the rail fares were £ 45 per person (single).
The cost for 30 days is:
And the cost for 33 days:
And we have:
So the increase is 9%.
There are larger solutions, for example:
But the room is described as “inexpensive”, so only the first of these is a viable solution.
However, they all involve a 9% increase.
LikeLike
GeoffR 12:34 pm on 29 October 2024 Permalink |
LikeLike
Ruud 4:15 am on 30 October 2024 Permalink |
from istr import istr istr.repr_mode("int") for train in istr(range(10, 100)): room = train[::-1] if room[0] != 0: total30 = int(train * 4 + room * 30) total33 = int(train * 4 + room * 33) increase = ((total33 - total30) / total30) * 100 if increase % 1 == 0: print(f"{train=} {room=} {total30=} {total33=} {increase=:.0f}%")LikeLike
Frits 5:43 am on 3 November 2024 Permalink |
Objective was to use fewer iterations.
from fractions import Fraction as RF # fare = 10 * f + g, room cost = 10 * g + f # t30 = 4*F + 30*R = 70*f + 304*g # t33 = 4*F + 33*R = 73*f + 334*g # 100 + p = 100 * (73*f + 334*g) / (70*f + 304*g) # integer percentages less than 10 for p in range(9, 0, -1): # (7300*f + 33400*g) - (100 + p) * (70*f + 304*g) = 0 # ((300 - p*70)*f + (3000 - 304*p)*g) = 0 # calculate - f / g (g will not be zero) r = RF(3000 - 304*p, 300 - p*70) if r > 0: continue f, g = abs(r.numerator), abs(r.denominator) if not (0 < f < 10 and g < 10): continue # double check if 100 * (73*f + 334*g) / (70*f + 304*g) - 100 != p: continue print(f"answer: {10 * g + f} pounds per night")LikeLike