Teaser 2512: [A square, a cube, a prime]
From The Sunday Times, 14th November 2010 [link] [link]
Using each of the digits 0 to 9 exactly once, I have written down two three-digit numbers and a four-digit 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?
This puzzle was originally published with no title.
[teaser2512]




Jim Randell 10:01 am on 4 November 2025 Permalink |
I wondered if we are meant to exclude numbers that fall into more than one category (i.e. 729 and 4096 are both squares and cubes). It turns out you get the same answer if you exclude them or not.
I recently extended the [[
choose()]] function in enigma.py, so that you can specify different collections for each choice, and we can use that functionality to solve this puzzle.The following Python program runs in 88ms. (Internal runtime is 11ms).
from enigma import (powers, primes, is_duplicate, choose, filter_unique, printf) # find 3 and 4 digit, squares, cubes, primes with no repeated digits def numbers(seq): (n3s, n4s) = (list(), list()) for n in seq: if n < 100: continue if n > 9999: break if is_duplicate(n): continue (n3s if n < 1000 else n4s).append(n) return (n3s, n4s) (sq3, sq4) = numbers(powers(10, 99, 2)) # squares (cb3, cb4) = numbers(powers(5, 21, 3)) # cubes (pr3, pr4) = numbers(primes.between(100, 9999)) # primes # find combinations of (square, cube, prime) that use 10 different digits check = lambda *ns: not is_duplicate(*ns) ss = list() for nss in [(sq3, cb3, pr4), (sq3, cb4, pr3), (sq4, cb3, pr3)]: ss.extend(choose(nss, check, multi_vs=1, multi_fns=0)) # find candidates unique by the category of the smallest number rs = filter_unique(ss, (lambda ss: ss.index(min(ss)))).unique # output solution for (sq, cb, pr) in rs: printf("square = {sq}, cube = {cb}, prime = {pr}")Solution: The numbers are: 324 (square), 6859 (cube), 701 (prime).
There are 19 candidate sets of numbers (or 16 if you remove those containing numbers that are both squares and cubes), but only one of them is uniquely identified if we are told that the square is the smallest number.
LikeLike
ruudvanderham 12:09 pm on 4 November 2025 Permalink |
import istr import collections squares = [i for i in istr.range(100, 10000) if i.all_distinct() and i.is_square()] cubes = [i for i in istr.range(100, 10000) if i.all_distinct() and i.is_cube()] primes = [i for i in istr.range(100, 10000) if i.all_distinct() and i.is_prime()] solutions = collections.defaultdict(list) for square in squares: for cube in cubes: for prime in primes: if len(square | cube | prime) == 10 and (square | cube | prime).all_distinct(): solutions[(square == min(square, cube, prime), cube == min(square, cube, prime), square == min(square, cube, prime))].append( (square, cube, prime) ) for solution in solutions.values(): if len(solution) == 1: print(solution[0])LikeLike