# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1659: Pairs of numbers

From New Scientist #2825, 13th August 2011 [link]

I have before me four two-digit numbers, all eight digits being different. I have grouped them into pairs, such that the product of the two numbers in each pair is the same, and is a perfect cube.

What are the four numbers?

[enigma1659]

### 3 responses to “Enigma 1659: Pairs of numbers”

1. jimrandell 4 December 2011 at 12:54 pm

The following Python code runs in 36ms.

```from itertools import permutations

# ab * cd = n^3 = ef * gh

# cubes up to 9801 = 99*99
cubes = list(pow(n, 3) for n in range(3, 22))

# let's assume a < c, e < g, a < e

d1 = set(range(0, 10))
for (a, b, c, d) in permutations(d1, 4):
if a in (0, 9): continue
if not(a < c): continue
ab = 10 * a + b
cd = 10 * c + d
n = ab * cd
if n not in cubes: continue

d2 = d1.difference((a, b, c, d))
for (e, f) in permutations(d2, 2):
if not(a < e): continue
ef = 10 * e + f

(gh, r) = divmod(n, ef)
if r: continue

(g, h) = divmod(gh, 10)
if not(0 < g < 10): continue
if not(e < g): continue
d3 = d2.difference((e, f))
if d3.intersection((g, h)): continue

print((ab, cd), (ef, gh), n, '=>', sorted((ab, cd, ef, gh)))
```

Solution: The four numbers are: 18 & 96 and 32 & 54.

2. geoffrounce 18 August 2016 at 8:23 am
```% A solution in MiniZinc
include "globals.mzn";

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; var 0..9:H;

var 10..99 : pair1 = A*10 + B;
var 10..99 : pair2 = C*10 + D;
var 10..99 : pair3 = E*10 + F;
var 10..99 : pair4 = G*10 + H;

set of int: cubes = {pow(x, 3) | x in 3..21};

constraint alldifferent([A,B,C,D,E,F,G,H])
/\ A > 0 /\ C > 0 /\ E > 0 /\ G > 0;

constraint pair1 * pair2 = pair3 * pair4 /\
pair1 * pair2 in cubes /\ pair3 * pair4 in cubes;

solve satisfy;

output ["Four numbers are : ",show(pair1),", ",show(pair2),
" and ",show(pair3),", ", show(pair4)];

% Four numbers are : 54,32 and 96,18
% Note: product of the two numbers in each pair = 1728 (12^3)
% Finished in 78msec

```
3. Jim Randell 18 August 2016 at 9:37 am

Here’s a solution using the general Alphametic solver (SubstitutedExpression()) from the enigma.py library.

Here is the command, along with the output. It runs in 93ms.

```% python -m enigma SubstitutedExpression \
--answer="((AB, CD), (EF, GH))" \
"is_cube(AB * CD)" \
"is_cube(EF * GH)" \
"AB * CD == EF * GH" \
"AB < CD" "EF < GH" "AB < EF"
(is_cube(AB * CD)) (is_cube(EF * GH)) (AB * CD == EF * GH) (AB < CD) (EF < GH) (AB < EF)
(is_cube(18 * 96)) (is_cube(32 * 54)) (18 * 96 == 32 * 54) (18 < 96) (32 < 54) (18 < 32) / A=1 B=8 C=9 D=6 E=3 F=2 G=5 H=4 / ((18, 96), (32, 54))
((AB, CD), (EF, GH)) = ((18, 96), (32, 54)) [1 solution]
```

The final line of checks is used to eliminate duplicate solutions, if we only want to find a solution then we can do without these checks and use the --first parameter to exit after the first solution is found, and this is also a bit faster (73ms).

```% python -m enigma SubstitutedExpression \
--first \
--answer="((AB, CD), (EF, GH))" \
"is_cube(AB * CD)" \
"is_cube(EF * GH)" \
"AB * CD == EF * GH"
(is_cube(AB * CD)) (is_cube(EF * GH)) (AB * CD == EF * GH)
(is_cube(18 * 96)) (is_cube(54 * 32)) (18 * 96 == 54 * 32) / A=1 B=8 C=9 D=6 E=5 F=4 G=3 H=2 / ((18, 96), (54, 32))
((AB, CD), (EF, GH)) = ((18, 96), (54, 32)) [1 solution]
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.