Sunday Times Teaser 2564 – 11/11/11
Published: 13 November 2011 (link)
As it was 11/11/11 last Friday, I have been looking at numbers that are divisible by 11.
I have written down an eight- figure number using the digits 2, 3, 4, 5, 6, 7, 8 and 9 in some order. For any two adjacent digits in the number, either one is a multiple of the other or the two digits differ by one.
My number is divisible by its first digit, its last digit, and (of course) by 11
What is the eight-figure number?
2 Comments
Leave one →
-
Brian Gladman permalink123456789101112131415from itertools import permutationsfrom functools import reduce# try all permutations of the 8 digitsfor p in permutations(range(2, 10), 8):# adjacent digits are either divisible or differ by oneif all(a % b == 0 or b % a == 0 or abs(a - b) == 1for a, b in zip(p, p[1:])):nbr = reduce(lambda x, y : 10 * x + y, p)# nbr is divisible by 11 and by its first and last digitsif not (nbr % 11 or nbr % p[0] or nbr % p[-1]):print('The number is {}.'.format(nbr))
-
geoffrounce permalink1234567891011121314151617181920from itertools import permutationsfor p in permutations((2,3,4,5,6,7,8,9)):# digits of the numbera, b, c, d, e, f, g, h = p# adjacent digits to check are ab, bc, cd, de, ef, fg and ghif not (a % b == 0 or b % a == 0 or abs(a - b)== 1): continueif not (b % c == 0 or c % b == 0 or abs(b - c)== 1): continueif not (c % d == 0 or d % c == 0 or abs(c - d)== 1): continueif not (d % e == 0 or e % d == 0 or abs(d - e)== 1): continueif not (e % f == 0 or f % e == 0 or abs(e - f)== 1): continueif not (f % g == 0 or g % f == 0 or abs(f - g)== 1): continueif not (g % h == 0 or h % g == 0 or abs(g - h)== 1): continue# now form the numbernum = (a + 10 * b + 100 * c + 1000 * d + 10000 * e+ 100000 * f + 1000000 * g + 10000000 * h)if num % h == 0 and num % a == 0 and num % 11 == 0:print('Number =', num)