Sunday Times Teaser 2568 – Ending 2011
by Adrian Somerfield
Published: 11 December 2011 (link)
I have written down three positive whole numbers consisting of two primes and a perfect square. Between them they use nine digits and no digit is repeated. If you form an addition sum with the three numbers, then what do you end up with as the total? Appropriately enough you end up with 2011. What, in increasing order, are the three numbers?
2 Comments
Leave one →

Brian Gladman permalink12345678910111213141516171819202122232425262728293031323334from itertools import combinationsfrom number_theory import Primes# a dictionary of perfect squares below 2012 with no repeated# digits, mapped to their sets of digitssq = {int(s): set(s) for s in (str(x * x) for x in range(1, 45))if len(set(s)) == len(s)}# a dictionary of primes below 2012 with no repeated digits,# mapped to their sets of digitspr = {int(s): set(s) for s in map(str, Primes(2012).list())if len(set(s)) == len(s)}# select the two prime valuesfor p1, p2 in combinations(pr, 2):# and check that they have no common digitsif not (pr[p1] & pr[p2]):# select the perfect squarefor s in sq:# and check it doesn't share any digits# with the two primesif not sq[s] & (pr[p1]  pr[p2]):# check that the sum of the numbers is 2011if s + p1 + p2 == 2011:t = ', '.join(str(x) for x in sorted((s, p1, p2)))h, s, t = t.rpartition(', ')print('The numbers are {}.'.format(' and '.join((h, t))))

GeoffR permalink123456789101112131415161718192021222324252627282930313233343536373839404142from itertools import permutationsdef is_sqr(n):return int(n ** 0.5 + 0.5) ** 2 == ndef is_prime(n):for x in range(2, int(n ** 0.5) + 1):if n % x == 0:return Falsereturn Truedigits = set(range(10))# Let abc be a perfect square and DEF and ghi be primes# 1st stage permutationfor p1 in permutations (digits, 3):a, b, c = p1abc = 100 * a + 10 * b + cif not is_sqr(abc):continue# 2nd stage permutationqs = digits.difference(p1)for p2 in permutations(qs,3):d, e, f = p2DEF = 100 * d + 10 * e + fif not is_prime(DEF):continue# 3rd stage permutationqs2 = qs.difference(p2)for p3 in permutations(qs2,3):g, h, i = p3ghi = 100 * g + 10 * h + iif not is_prime(ghi):continueif abc + DEF + ghi == 2011:t = tuple(sorted((abc, DEF, ghi)))print(f"Three numbers are {t[0]}, {t[1]} and {t[2]}")