Teaser 2515: [Interleaved powers]
From The Sunday Times, 5th December 2010 [link] [link]
Using each digit just once I wrote down two five-figure numbers. One was a cube and the other was a square. Then I combined these two five-figure numbers to give me a ten-figure number by using one of them to occupy the odd positions (from left to right) and the other to occupy the even positions (again from left to right). Then the four-figure number made up in order from the digits in the prime positions turned out to be prime.
What was the ten-figure number?
This puzzle was originally published with no title.
[teaser2515]
Jim Randell 9:39 am on 3 October 2025 Permalink |
If we suppose the square number is ABCDE, and the cube is FGHIJ then the combined number is one of:
And the corresponding prime positions (2nd, 3rd, 5th, 7th) form a prime.
So the prime is:
Here is a straightforward solution using the [[
SubstitutedExpression()]] solver from the enigma.py library.It runs in 114ms. (Internal runtime is 43ms).
from enigma import (SubstitutedExpression, is_prime, printf) # use the SubstitutedExpression solver to find the square and the cube p = SubstitutedExpression( ["is_square(ABCDE)", "is_cube(FGHIJ)"], answer="(ABCDE, FGHIJ, AFBGCHDIEJ, FBCD, FAGBHCIDJE, AGHI)", # [optional] squares cannot end in 2, 3, 7, 8 d2i={ 0: 'AF', 2: 'E', 3: 'E', 7: 'E', 8: 'E' } ) # look for viable solutions for (sq, cb, n1, p1, n2, p2) in p.answers(verbose=0): (f1, f2) = (is_prime(p1), is_prime(p2)) if f1 or f2: printf("square = {sq}, cube = {cb}") if f1: printf("-> n = {n1}, prime = {p1}") if f2: printf("-> n = {n2}, prime = {p2}") printf()Solution: The 10-digit number is: 5204137869.
So the 4-digit prime is 2017, the odd digits form 50176 (= 224^2), and the even digits form 24389 (= 29^3).
For a faster runtime we can collect the squares and cubes up front.
The following Python program has an internal runtime of 479µs.
from enigma import ( defaultdict, irange, sq, cb, diff, cproduct, interleave, restrict, is_prime, join, printf ) # index numbers (with distinct digits) by digit content def numbers(seq): d = defaultdict(list) for n in seq: s = str(n) # check digits are distinct if len(s) != len(set(s)): continue d[join(sorted(s))].append(s) return d # find candidate squares and cubes sqs = numbers(sq(x) for x in irange(100, 316)) cbs = numbers(cb(x) for x in irange(22, 46)) # consider possible cubes for (kc, vs) in cbs.items(): # and look for corresponding squares ks = join(diff("0123456789", kc)) for (square, cube) in cproduct([sqs[ks], vs]): # look for possible interleavings for (a, b) in [(square, cube), (cube, square)]: n = join(interleave(a, b)) # extract the digits in (1-indexed) prime positions p = int(restrict(n, [1, 2, 4, 6])) if is_prime(p): printf("square = {square}, cube = {cube} -> n = {n}, p = {p}")LikeLike