Teaser 2577: Number waves
From The Sunday Times, 12th February 2012 [link] [link]
I challenged Sam to find a 9-digit number using all of the digits 1 to 9, such that each digit occupying an even position was equal to the sum of its adjacent digits. (For example, the digit occupying position four would be equal to the sum of the digits occupying positions three and five). Sam produced a solution that was exactly divisible by its first digit, by its last digit, and by 11.
What was his number?
[teaser2577]
Jim Randell 5:08 pm on 13 June 2024 Permalink |
Here is a solution using the [[
SubstitutedExpression]] solver from the enigma.py library.It runs in 77ms. (Internal runtime of the generated program is 230µs).
Run: [ @replit ]
Solution: Sam’s number was: 143968275.
LikeLike
Ruud 7:49 pm on 15 June 2024 Permalink |
No a priori knowledge, just brute force:
from istr import istr for i in istr.concat(istr.permutations(istr.digits("1-"), 9)): if ( i[1] == i[0] + i[2] and i[3] == i[2] + i[4] and i[5] == i[4] + i[6] and i[7] == i[6] + i[8] and i.divisible_by(11) and i.divisible_by(i[0]) and i.divisible_by(i[8]) ): print(i)Note that this requires the latest-soon to be published- version of istr.
LikeLike
GeoffR 6:56 pm on 13 June 2024 Permalink |
LikeLike
Frits 7:01 pm on 13 June 2024 Permalink |
LikeLike
Frits 9:23 pm on 13 June 2024 Permalink |
You only need to know three digits (like the 1st, 3rd and 5th digits).
from functools import reduce # convert digit sequences to number d2n = lambda s: reduce(lambda x, y: 10 * x + y, s) dgts8 = set(range(1, 9)) dgts9 = dgts8 | {9} for C in dgts8: for E in dgts8 - {C}: # 11 divisibility rule G = (11 if C + E < 11 else 22) - (C + E) D, F = C + E, E + G # different digits if len(r1 := dgts9 - {C, E, G, D, F}) != 4: continue for A in r1: B = A + C if len(r2 := sorted(r1 - {A, B})) != 2: continue H, I = r2[1], r2[0] if H != G + I: continue ABCDEFGHI = d2n([A, B, C, D, E, F, G, H, I]) if ABCDEFGHI % A: continue if ABCDEFGHI % I: continue print("answer:", ABCDEFGHI)LikeLike
Ruud 6:44 am on 16 June 2024 Permalink |
This version does not require the divisible_by method:
from istr import istr for i in istr.concat(istr.permutations(istr.digits("1-"), 9)): if i[1] == i[0] + i[2] and i[3] == i[2] + i[4] and i[5] == i[4] + i[6] and i[7] == i[6] + i[8] and i % 11 == 0 and i % i[0] == 0 and i % i[8] == 0: print(i)LikeLike