# Enigmatic Code

Programming Enigma Puzzles

## Puzzle 77: Letters for digits: a multiplication

From New Scientist #1128, 9th November 1978 [link]

In the multiplication sum below the digits have been replaced by letters. The same letter stands for the same digit whenever it appears, and different letters stand for different digits.

Write the sum out with letters replaced by digits.

[puzzle77]

### 3 responses to “Puzzle 77: Letters for digits: a multiplication”

1. Jim Randell 29 March 2017 at 9:08 am

It doesn’t take long to solve this puzzle by hand.

Nor does it take long to solve it using the SubstitutedExpression() solver from the enigma.py library. We can solve it directly from the command line. Here’s the command and its output. It runs in 99ms.

```% python -m enigma SubstitutedExpression "YMGYBMP * P = PAXHEBY"
(YMGYBMP * P = PAXHEBY)
(1041809 * 9 = 9376281) / A=3 B=8 E=2 G=4 H=6 M=0 P=9 X=7 Y=1
[1 solution]
```

Solution: The sum is: 1041809 × 9 = 9376281.

2. geoffrounce 29 March 2017 at 4:54 pm

A Python solution runs in 316 msec

```digits = set('1234567890')

from itertools import permutations

for p1 in permutations('1234567890',5):
y, m, g, b, p = p1
if y == '0' or p == '0': continue
# form  1st number
ymgybmp = int(y + m + g + y + b + m + p)

p2 = digits.difference(p1)
for q in permutations(p2, 4):
a, x, h, e = q
# form 2nd number
paxheby = int(p + a + x + h + e + b +  y)
P = int(p)
if ymgybmp * P == paxheby:
print('{} * {} = {}'.format(ymgybmp, P, paxheby))
```

A MiniZinc solution runs in 104 msec

```% A solution in MiniZinc
include "globals.mzn";

var 1..9:Y;  var 0..9:M;  var 0..9:G;  var 0..9:B;  var 1..9:P;
var 0..9:A;  var 0..9:X;  var 0..9:H;  var 0..9:E;

constraint alldifferent([Y,M,G,B,P,A,X,H,E]);

var 1000000..9999999: YMGYBMP = P + 10*M + 100*B + 1000*Y + 10000*G + 100000*M + 1000000*Y;
var 1000000..9999999: PAXHEBY = Y + 10*B + 100*E + 1000*H + 10000*X + 100000*A + 1000000*P;

constraint YMGYBMP * P == PAXHEBY;

solve satisfy;

output [ show(YMGYBMP), " * ", show(P), " = ", show(PAXHEBY) ];

% 1041809 * 9 = 9376281
% Finished in 104msec
```
• Brian Gladman 29 March 2017 at 9:30 pm

Since we are all using different environments to run our code, I really don’t believe the timing data here has much if any significance. But since we are giving timing data, this one runs in less than 2ms using profile based timing and less than 100ms total in Windows Powershell on my laptop (3GHz Intel i7-6920).

```from itertools import permutations
from functools import reduce

# Y = P^2 mod 10, P * Y < 10, Y != P ==> (P, Y) = (2, 4) or (9, 1)
for P, Y in ((2, 4), (9, 1)):

# partial computation of the larger multiplier
t = 1001000 * Y + P

# choose digits for the remaining letters of the larger multiplier
for M, G, B in permutations(set(range(10)).difference([P, Y]), 3):

# complete the value of the large multiplier and compute the result
top = t + 100010 * M + 10000 * G + 100 * B
p, A, X, H, E, b, y, *r = (int(x) for x in str(P * top))

# check that duplicated digits match and for the right number of
# different values for the letters
if not r and (p, b, y) == (P, B, Y):
if len(set([A, B, E, G, H, M, P, X, Y])) == 9:
print(f'{P} x {top} = {P * top}')
```