Teaser 3053: Endless number
From The Sunday Times, 28th March 2021 [link] [link]
George and Martha have a telephone number consisting of nine digits; there is no zero and the others appear once each. The total of the digits is obviously 45, so that the number is divisible by nine. Martha noticed that, if she removed the last (i.e., the least significant) digit, an eight-digit number would remain, divisible by eight. George added that you could continue this process, removing the least significant digit each time to be left with an n-digit number divisible by n right down to the end.
What is their telephone number?
[teaser3053]
Jim Randell 5:04 pm on 26 March 2021 Permalink |
See also: Enigma 339b, Enigma 1643, Enigma 1667, and the recently posed Puzzle #102.
Here’s a solution using the [[
SubstitutedExpression]] solver from the enigma.py library. It runs in 96ms.Solution: The number is 381654729.
LikeLike
Frits 10:40 pm on 26 March 2021 Permalink |
Tested in a Python online editor.
from itertools import permutations print(*["".join(x) for x in permutations("123456789") if all(int("".join(x[:i])) % i == 0 for i in range(2, 9))])LikeLike
Tony Brooke-Taylor 9:02 pm on 28 March 2021 Permalink |
I used a few shortcuts with the aim of minimising wasted iterations. Running my code on Replit takes about 3.5ms, compared with 1.13s for Frits’ sledgehammer approach. On the other hand, mine takes a few more lines!
Hope I manage to paste this in an appropriate format:
evens=list(range(2,9,2)) Xs=list(range(2,9,4)) odds=list(range(1,10,2)) odds.remove(5) #Exploit the fact that the three-digit and six-digit numbers must be divisible by 3, and that the 6-digit number can be expressed as the sum of ABC*1000 and XYZ, so the digits of both ABC and XYZ must sum separately to 3. Let the last three digits be RST. #For a four-digit number to be divisible by four, the last two digits must make a number divisible by four. Therefore if the third digit is odd, the fourth must be an odd multiple of 2 for X in Xs: vec=[None for i in range(9)] vec[4]=5#no zeros allowed, and the five-digit number divisible by 5 vec[3]=X Z=10-X#consequence of sum(X,Y,Z) divisible by three vec[5]=Z Bs=[i for i in evens if i not in vec]#remaining evens go to 2nd and 8th for B in Bs: vec[1]=B S=Bs[(Bs.index(B)-1)%2] vec[7]=S #Try each pair of odd numbers (other than 5) in positions 1 and 3, subject to requiring that ABC divisible by 3 (requiring A+B+C divisible by 3) for A in odds: Cs=odds.copy() Cs.remove(A) for C in Cs: if sum([A,B,C])%3==0: vec[0]=A vec[2]=C #Complementary odds then fall into positions 7 and 9 Rs=Cs.copy() Rs.remove(C) for R in Rs: vec[6]=R if sum([i*10**(6-vec.index(i)) for i in vec[:7]])%7==0 and sum([i*10**(7-vec.index(i)) for i in vec[:8]])%8==0: Ts=Rs.copy() Ts.remove(R) for T in Ts: vec[8]=T print("The telephone number is",sum([i*10**(8-vec.index(i)) for i in vec[:9]]))LikeLike
Frits 10:57 am on 29 March 2021 Permalink |
Hi Tony,
A couple of recommendations:
index() is expensive so line 35 may be written to :
You can also use something like “dgts2nbr” as in puzzle [[ https://enigmaticcode.wordpress.com/2017/04/10/enigma-1120-assorted-numbers/ ]]
– line 16-19 can be rewritten to:
or
or
LikeLike
Tony 6:01 pm on 29 March 2021 Permalink |
Thanks Frits: now I know about enumerate and reduce. As I’m getting the hang of looping over iterables I had forgotten it is still possible to loop with a counter, so I would probably choose your second approach to lines 16-19. I was trying to avoid using itertools once I realised I only needed to deal with pairs.
LikeLike
Hugh Casement 12:51 pm on 27 March 2021 Permalink |
Isn’t this effectively the same as Brainteaser 1040 (4 July 1982)?
That one at least spared us George and Martha, who get dragged in again and again for no good reason.
LikeLike
Jim Randell 3:36 pm on 27 March 2021 Permalink |
@Hugh: It does seem to be. I suppose it is hard to come up with over 3000 puzzles without some overlap. Or to check that there is no overlap!
LikeLike
Hugh Casement 1:14 pm on 30 March 2021 Permalink |
And the recent Puzzle 102 in New Scientist. I call that plagiarism!
LikeLike
GeoffR 1:39 pm on 20 May 2021 Permalink |
from itertools import permutations from enigma import join # the number 45 is divisible by nine i = 9 for p in permutations('123456789', 8): a, b, c, d, e, f, g, h = p # A geometric pattern to this code if int(a + b + c + d + e + f + g + h) % 8 == 0: if int(a + b + c + d + e + f + g) % 7 == 0: if int(a + b + c + d + e + f) % 6 == 0: if int(a + b + c + d + e) % 5 == 0: if int(a + b + c + d) % 4 == 0: if int(a + b + c) % 3 == 0: if int(a + b) % 2 == 0: tel_num = join((a, b, \ c, d, e, f, g, h, i)) print('No. is',tel_num) # No. is 381654729LikeLike
Dirk 9:43 am on 9 July 2021 Permalink |
Teaser 1882 (11/10/1998): Multiple Solutions by Rex Gooch is also linked with brainteaser 1040/3053.
LikeLike