Riddler Express
This time around, there are six three-digit numbers — each belongs in a row of the table below, with one digit per cell. The products of the three digits of each number are shown in the rightmost column. Meanwhile, the products of the digits in the hundreds, tens and ones places, respectively, are shown in the bottom row. Can you find all six three-digit numbers and complete the table?
Computational Solution
from z3 import *
class MysteriousNumbersSolver:
def __init__(self):
self.X = [[Int("self.X_%s_%s" % (i, j)) for j in range(3)]
for i in range(6)]
self.s = Solver()
self.s.add([And(0 <= self.X[i][j], self.X[i][j]<= 9) for i in range(6) for j in range(1,3)])
self.s.add([And(0 <= self.X[i][0], self.X[i][0]<= 9) for i in range(6)])
def set_constraints(self):
self.s.add(self.X[0][0]*self.X[0][1]*self.X[0][2]==210)
self.s.add(self.X[1][0]*self.X[1][1]*self.X[1][2]==144)
self.s.add(self.X[2][0]*self.X[2][1]*self.X[2][2]==54)
self.s.add(self.X[3][0]*self.X[3][1]*self.X[3][2]==135)
self.s.add(self.X[4][0]*self.X[4][1]*self.X[4][2]==4)
self.s.add(self.X[5][0]*self.X[5][1]*self.X[5][2]==49)
self.s.add(self.X[0][0]*self.X[1][0]*self.X[2][0]*self.X[3][0]*self.X[4][0]*self.X[5][0]==6615)
self.s.add(self.X[0][1]*self.X[1][1]*self.X[2][1]*self.X[3][1]*self.X[4][1]*self.X[5][1]==15552)
self.s.add(self.X[0][2]*self.X[1][2]*self.X[2][2]*self.X[3][2]*self.X[4][2]*self.X[5][2]==420)
def output_solution(self):
= self.s.model()
m for i in range(6):
print(" ".join([str(m.evaluate(self.X[i][j])) for j in range(3)]))
def solve(self):
self.set_constraints()
if self.s.check() == sat:
self.output_solution()
else:
print(self.s)
print("Failed to solve.")
= MysteriousNumbersSolver()
s s.solve()
Here is the solution
7 6 5
9 8 2
3 9 2
5 9 3
1 4 1
7 1 7