# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1353: Three cubed

From New Scientist #2512, 13th August 2005

Harry, Tom and I each looked to replace each asterisk with a digit in such a way that the two 4-digit numbers and one 3-digit number that could be read across and the 5-digit number that could be read down were all perfect cubes (with no leading zeros).

We each found a different solution that used nine of the ten digits 0-9. Harry’s unused digit was the same as Tom’s.

What was my solution?

[enigma1353]

### 3 responses to “Enigma 1353: Three cubed”

1. Jim Randell 24 January 2014 at 8:38 am

This Python program runs in 35ms.

```from collections import defaultdict
from itertools import permutations
from enigma import irange, printf

# 3, 4, 5 digit cubes
cubes = defaultdict(list)
for i in irange(5, 46):
n = str(i ** 3)
cubes[len(n)].append(n)

# digits
ds = set('0123456789')

# find possible arrangements: record (digit, cubes)
r = list()

# choose a 5 digit cube
for c5 in cubes[5]:
# and a 3 digit cube
for c3 in cubes[3]:
if c3[-1] != c5[2]: continue
# and the top 4 digit cube
for c4t in cubes[4]:
if c4t[-1] != c5[0]: continue
# and the bottom 4 digit cube
for c4b in cubes[4]:
if c4b[-1] != c5[-1]: continue
# we're interested in arrangements that use 9 digits
d = ds.difference(c5 + c3 + c4t + c4b)
if len(d) != 1: continue
r.append((d.pop(), (c5, c3, c4t, c4b)))

# find solutions
for (T, D, H) in permutations(r, 3):
# H and T's unused digits are the same
if H[0] == T[0]:
printf("D={D} [T={T} / H={H}]", D=' '.join(D[1]), T=' '.join(T[1]), H=' '.join(H[1]))
```

Solution: Dick’s solution is:

```4096
8
729
2
1331```

There are only three solutions that use exactly 9 digits. The one given above doesn’t use the digit 5, the remaining two solutions (Harry’s and Tom’s) don’t use the digit 8:

```1331    3375
7       0
125     216
7       5
4096    4913```
2. Naim Uygun 24 January 2014 at 10:43 am
```"""
1331 7 125 7 4096 17576
The unused digit: {'8'}

3375 0 216 5 4913 50653
The unused digit: {'8'}

Answer: 4096 8 729 2 1331 68921
The unused digit: {'5'}
"""
d=set(str(i) for i in range(0,10))
cube3=[str(x**3) for x in range(5,10)]
cube4=[str(x**3) for x in range(10,22)]
cube5=[str(x**3) for x in range(22,47)]
for one in cube4:
for two in cube4:
for three in cube3 :
for five in cube5:
if one[3] != five[0]: continue
if three[2] != five[2]: continue
if two[3] != five[4]: continue
if '0'==five[0] :continue
s=set(list(one+two+three+five))
if len(s)!= 9: continue
print("\n",one,five[1],three,five[3],two,five)
print("The unused digit:",d-s)
```
3. geoffrounce 15 February 2018 at 2:52 pm
```% A Solution in MiniZinc
include "globals.mzn";

%     A a b B
%           c
%       D d C
%           e
%     E f g F

var 0..9: A;   var 0..9: B;   var 0..9: C;   var 0..9: D;
var 0..9: E;   var 0..9: F;

var 0..9: a;   var 0..9: b;   var 0..9: c;   var 0..9: d;
var 0..9: e;   var 0..9: f;    var 0..9: g;

% No leading zero digits
constraint A != 0 /\ B != 0 /\ D != 0 /\ E != 0;

% Nine of the ten digits 0-9 only are used
constraint card (  {A, B, C, D, E, F, a, b, c, d, e ,f, g}  ) == 9;

% Cube numbers
var 100..999: DdC = 100*D + 10*d + C;
var 1000..9999: AabB = 1000*A + 100*a + 10*b + B;
var 1000..9999: EfgF = 1000*E + 100*f + 10*g + F;
var 10000..99999: BcCeF = 10000*B + 1000*c + 100*C + 10*e + F;

% Two four digit numbers are different
constraint AabB != EfgF;

set of int: cb3 = {n*n*n | n in 5..9};
set of int: cb4 = {n*n*n | n in 10..21};
set of int: cb5 = {n*n*n | n in 22..46};

% All numbers must be cubes
constraint DdC in cb3 /\ AabB in cb4 /\ EfgF in cb4 /\ BcCeF in cb5;

solve satisfy;

output[ "Cubes are: " ++show(DdC) ++ ",  " ++ show(AabB) ++ ",  " ++ show(EfgF)
++ ",  " ++ show(BcCeF) ];

% Cubes are: 125,  1331,  4096,  17576  -  Digit 8 unused - Harry or Tom's numbers
% Cubes are: 216 , 3375,  4913,  50653 -   Digit 8 unused - Harry or Tom's numbers
% Cubes are: 729,  4096,  1331,  68921 -   Digit 5 unused - Dick's numbers
% ----------
% ==========
% Finished in 118msec
```