From New Scientist #1922, 23rd April 1994 [link]
Harry’s safe has a combination lock, with the usual dial with divisions marked round its edge from 0 to 99. Security rules forbid him to write down the combination, which he is expected to memorise. He has a poor memory for figures, so he can seldom remember it, but he can remember that it has four 2-digit numbers and one 1-digit number in sequence, and that if the sequence of numbers is strung together, the digits of the resulting 9-digit number are all different. He can also remember that if he subtracts his room number from that number, the result is a perfect square; of which the square root is a perfect cube; of which the cube root is his room number. He would not of course tell you all this, because he knows that it would enable you to work out the combination.
Reminding himself of his room number (which is on his door!) he can work out the combination in a few minutes, even without his pocket calculator (which reduces the time to mere seconds). He thus avoids the ignominy of having to plead with Security to open the safe for him.
You may take rather longer than Harry to work out the combination; but if I tell you that the sum of the digits of the cube is equal to its cube root, it could help cut the time.
What is the combination (the five separate numbers in sequence)?
[enigma767]
This Python program runs in 57ms. (Internal runtime is 137µs).
Run: [ @replit ]
Solution: The combination is: 38, 74, 20, 51, 6.
The room number is 27, and so the combination is determined from the calculation:
As @Jim Randell had also worked out:
code = room_number ** 6 + room_number.
And as there’s nowhere stated that the individual 2- and 1-digit numbers may not start with a 0, there’s no need to verify that (as @Jim Randell does in his code).
Therefore, the code can be really simple:
And indeed this gives two possible answers:
code = 02-41-37-58-6 room number=17
code = 38-74-20-51-6 room number=27
Observation: The search stops at the moment room_number becomes 32.
Note that it would be possible to determine the first possible start number (being 15), but I don’t think it’s worth the effort.
Actually the test len(code) == 9 is not required!
So the last test line could also read:
I had forgotten the last hint
“the sum of the digits of the cube is equal to its cube root”
, but if I add that test, my two solutions are still ok.
Here’s my updated code:
@Ruud: In these puzzles leading zeros are not usually allowed unless explicitly stated. So in this puzzle (as in standard usage) a 1-digit number is 0 – 9, a 2-digit number 10 – 99, etc.
If you take this into account you should find there is a single unique solution to the puzzle.
Assuming a 2-digit room number (as shown above by @GeoffR), we can use a simple [[
SubstitutedExpression
]] recipe to solve the puzzle.The following run file has an internal runtime of 78µs.
Run: [ @replit ]
Faster in Chuffed Solver than Geocode Solver, but much slower than Python.
Only fast with Geocode Solver. Variable X has to be limited to 31 otherwise there is an “out of range” error. I didn’t code the optional constraint (“to help cut the time”).