Sunday Times Teaser 2577 – Number Waves
by Graham Smithers
Published: 12 February 2012 (link)
I challenged Sam to find a 9-figure 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?
2 Comments
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829303132from functools import reducedef solve(seq=[]):ln = len(seq)# do we have nine digits?if ln == 9:yield seq# are we adding a digit in an odd position# (counting from the units digit as odd)elif ln and not ln % 2:# we need a value that makes its sum with the last# but one digit equal to the previous digitv = seq[-1] - seq[-2]# check it is a valid digit and not already presentif 0 <= v < 10 and v not in seq:yield from solve(seq + [v])else:# otherwise add any unused digit (except 0 as a# leading digit)s = set(range(0 if len(seq) else 1, 10))for d in s.difference(seq):yield from solve(seq + [d])for s in solve():# form the numbernbr = reduce(lambda x, y: 10 * x + y, s)# the last digit cannot be zeroif s[-1]:if not (nbr % s[0] or nbr % s[-1] or nbr % 11):print('His number was {}.'.format(nbr))
-
geoffrounce permalink1234567891011from itertools import permutationsfor p in permutations('123456789',9):a,b,c,d,e,f,g,h,i = pnum = int(a+b+c+d+e+f+g+h+i)if int(b) == int(a) + int(c) and int(d) == int(c) + int(e)and int(f) == int(e) + int(g) and int(h) == int(g) + int(i):if num % int(a) == 0 and num % int(i) == 0 and num % 11 == 0:print('Number = ', num)