# PUZZLE WORLD alphametic

I have tried to solve this but I can't. Does somebody know the answer?

Ref: Puzzle World, Ed. Nob Yoshigahara and Richard Bozulich, Ishi Press, Summer 1992, Pilot Edition.

• So, we have to fill in the "x"? – Sid Aug 21 '16 at 14:27
• Does each letter (including x) represent a different digit? – Shuri2060 Aug 21 '16 at 14:44
• x can be same or diffeent digit and this is a question of puzzle and world – smtith park Aug 21 '16 at 14:59
• I'm afraid there is no solution in which P, U, Z, L, E, W, O, R, and D are all different digits, and leading zeroes are not allowed. I cannot prove it, but I wrote a program to brute force it (it was quite tricky to do it effectively). No solution found with these criteria. – elias Aug 21 '16 at 20:46
• @QuestionAsker If all the x digits in the multiplicand are equal then all the intermediate sum values would all be equal (post-left-shift) too, so all of W,O,R,L,D would all be equal to x too, but P,U,Z,L,E,W,O,R,D are surely meant to be unique digits (otherwise we could just say 0 * 0 = 0); so it must be that any x is free to be any digit (elias suggests it may only be possible if some leading digits are also 0) – Jonathan Allan Aug 22 '16 at 0:46

Since this does not have the tag I have used a computer to

find the single solution
- with P,U,Z,L,E,W,O,R,D being unique digits in $[0,9]$, and
- all instances of x free to be any digit.

That solution is P,U,Z,L,E,W,O,R,D = 6,9,5,0,3,7,8,2,1.

Numerically on the left, and with the character X for 4 on the right:

       695503               PUZZLE
6684445              PPOXXXZ
------- *            ------- *
3477515              EXWWZDZ
2782012              RWORLDR
2782012              RWORLDR
2782012              RWORLDR
5564024              ZZPXLRX
4173018              XDWELDO
4173018              XDWELDO
------------- +      ------------- +
4649051550835        XPXULZDZZLOEZ

I did it in a relatively similar way to a manual approach

I haven't made all the inferences I would make manually, but I have reduced the space in steps that would probably be used as part of a manual approach (one would prune more of the space than we need to with a computer, since they calculate so fast):

If one labels the multiplicand xxxxxxx as x6,x5,x4,x3,x2,x1,x0 then the innermost generator (the third function) looks for values of P,U,Z,Z,L,E (with unique digits P,U,Z,L,E) and x3 (in $[0,9]$) that produce a valid x,W,O,R,L,D,x intermediate sum (with unique digits P,U,Z,L,E,W,O,R,D) - there are $242$ such choices.

The generator that wraps around that (the second function) then runs through these choices and looks for values of x5,x4,x2,x1 that produce intermediate sums with the matching W,O,L,D - this yields $2,315$ choices.

The generator that wraps around that (the first function) then runs through these choices and looks for values of x6,x0 that produce a final result matching x,P,x,U,x,Z,x,Z,x,L,x,E,x - this yields a single result (the two multiplicands as stings).

The Python code:

from itertools import combinations, permutations

def iterSolutions():
for puzzle, puzzleInt, x1, x2, x3, x4, x5 in iterValid_Puzzle_PuzzleInt_X1_X2_X3_X4_X5():
p, u, z, z, l, e = puzzle
pX = (10 * x1 + 100 * x2 + 1000 * x3 + 10000 * x4 + 100000 * x5)
for x6 in range(10):
pR = puzzleInt * (1000000 * x6 + pX)
for x0 in range(10):
r = str(puzzleInt * x0 + pR)
newP = len(r) > 11 and r[-12] or '0'
if newP == p:
newU = len(r) > 9 and r[-10] or '0'
if newU == u:
newZ = len(r) > 7 and r[-8] or '0'
if newZ == z:
newZ = len(r) > 5 and r[-6] or '0'
if newZ == z:
newL = len(r) > 3 and r[-4] or '0'
if newL == l:
newE = len(r) > 1 and r[-2] or '0'
if newE == e:
yield puzzle, ''.join(str(v) for v in (x6, x5, x4, x3, x2, x1, x0))

def iterValid_Puzzle_PuzzleInt_X1_X2_X3_X4_X5():
for puzzle, puzzleInt, x3, w, o, d in iterValid_Puzzle_PuzzleInt_X3_W_O_D():
for x1 in range(10):
s1 = str(puzzleInt * x1)
wNew = len(s1) > 5 and s1[-6] or '0'
if wNew == w:
for x2 in range(10):
s2 = str(puzzleInt * x2)
oNew = len(s2) > 4 and s2[-5] or '0'
if oNew == o:
l = puzzle[4]
for x4 in range(10):
s4 = str(puzzleInt * x4)
lNew = len(s4) > 2 and s4[-3] or '0'
if lNew == l:
for x5 in range(10):
s5 = str(puzzleInt * x5)
dNew = len(s5) > 1 and s5[-2] or '0'
if dNew == d:
yield puzzle, puzzleInt, x1, x2, x3, x4, x5

def iterValid_Puzzle_PuzzleInt_X3_W_O_D():
for puzzleSelection in combinations('9876543210', 5):
for p,u,z,l,e in permutations(puzzleSelection):
puzzle = ''.join((p,u,z,z,l,e))
puzzleSet = set(puzzle)
puzzleInt = int(puzzle)
for x3 in range(10):
s3 = str(puzzleInt * x3)
if len(s3) == 5:
w = '0'
elif len(s3) < 5:
continue
else:
w = s3[-6]
d,l,r,o = s3[-2:-6:-1]
if l != puzzle[4]:
continue
if len(puzzleSet | set((w,o,r,d))) == 9:
yield puzzle, puzzleInt, x3, w, o, d

Running that code (sub-second):

>>> for solution in iterSolutions():
...     solution
...
('695503', '6684445')
>>>
Which is saying the only solution is 695503 * 6684445, the multiplication I showed above.

• Great answer! I used a very similar - almost identical approach, but it looks like I've made a bug. Now I have to debug my spaghetti-code... – elias Aug 22 '16 at 6:17