Sunday Times Teaser 2527
by Graham Smithers
Published: 27 February 2011 (link)
Without repeating a digit I wrote down two five-figure numbers. For the first of these, its first two digits form a number divisible by two, its first three digits form a number divisible by three, and likewise for four and five. For the second number, looking again at the numbers formed by the first few digits, those of prime length are prime and the one of length four is a square. Furthermore the sum of the digits of the difference between the two numbers is itself a digit. Without doing much division you should now be able to answer the question:
What are the two five-figure numbers?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930from itertools import permutationsfrom functools import reducefrom number_theory import is_prime# find possible second numbers firstfor p in permutations(range(10), 5):if p[0]:nbr1 = reduce(lambda x, y: 10 * x + y, p)# test for the four digit square and the five digit primert = int((nbr1 // 10) ** 0.5)if rt ** 2 == nbr1 // 10 and is_prime(nbr1):# test for the two and three digit primesif is_prime(nbr1 // 1000) and is_prime(nbr1 // 100):# now find the possible first numbersfor q in permutations(set(range(10)).difference(p)):if q[0]:nbr2 = reduce(lambda x, y: 10 * x + y, q)# test that the first N (2 .. 5) digits are divisible by Nif not any((nbr2 // 10 ** (5 - n)) % n for n in range(1, 6)):# find the sum of the digits in the difference between# the numbers and check that it is in the second numbersm = sum(map(int, str(abs(nbr1 - nbr2))))if sm in p:print('The numbers are {} and {}.'.format(nbr2, nbr1))