Sunday Times Teaser 2512 – No Title
by Graham Smithers
Published November 14 2010 (link)
Using each of the digits 0 to 9 exactly once, I have written down two three-figure numbers and a four- figure number.
In no particular order, one of them is a perfect square, one is a perfect cube, and the other is a prime.
If I told you which of the square, the cube and the prime was the smallest then it would be possible for you to work out what those three numbers are.
What are they?
One Comment
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344from collections import defaultdictfrom number_theory import Primes# return the digits in a numbern2d = lambda n:tuple(int(c) for c in str(n))# map three and four digit squares to their digits, omitting# those with duplicate digitssqrs = [(n * n, n2d(n * n)) for n in range(10, 100)]sqrs = {k:set(v) for k, v in sqrs if len(v) == len(set(v))}# map three and four digit cubes to their digits, omitting# those with duplicate digitscbes = [(n ** 3, n2d(n ** 3)) for n in range(5, 22)]cbes = {k:set(v) for k, v in cbes if len(v) == len(set(v))}# map the digit sets of three and four digit primes to the# primes, omitting those with duplicate digitsd2p = defaultdict(list)for pp in Primes().range(100, 10000):pd = n2d(pp)if len(pd) == len(set(pd)):d2p[frozenset(pd)].append(pp)# map the type (0 = square, 1 = cube, 2 = prime) of the# smallest value in triples to triplesd = defaultdict(list)# for all square valuesfor ss, sd in sqrs.items():# find cube values that do not share digits with themfor cc, cd in cbes.items():if not (sd & cd):# find primes that use the remaining digitsfor pp in d2p[frozenset(set(range(10)).difference(sd | cd))]:scp = (ss, cc, pp)d[scp.index(min(scp))].append(scp)# find the type of the smallest value that gives a unique solutionfor k, v in d.items():if len(v) == 1:(s, c, p), = vprint(f'square = {s}, cube = {c}, prime = {p}')