Sunday Times Teaser 2584 – Truncation
by Andrew Skidmore
Published: 1 April 2012 (link)
Callum is learning about squares and primes. He told me that he had discovered a four-figure square that becomes a three-figure prime if you delete its last digit. I could not work out his square from this information and he informed me that, if I knew the sum of its digits, then I would be able to work it out. So I then asked whether the square had a repeated digit: his answer enabled me to work out his square.
What was it?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627from collections import defaultdictfrom number_theory import Primes# list four digit squares that are primes when their last digit is removedsq = [x ** 2 for x in range(32, 100) if x ** 2 // 10 in Primes(1000)]d = defaultdict(list)for s in sq:# list primes with common sums of digitsd[sum(int(c) for c in str(s))] += [s]# form a list of results whose sum of digits is uniquel = [d[s][0] for s in d if len(d[s]) == 1]# list squares with no repeated digitsno_rep = [s for s in l if len(str(s)) == len(set(str(s)))]# and with repeated digitsrep = [s for s in l if len(str(s)) != len(set(str(s)))]# find the item in the list with only one itemr = rep[0] if len(rep) == 1 else no_rep[0] if len(no_rep) == 1 else Noneif r:print('The square is', r)else:print('No solution')