3-Digit Math Problem

Let us solve the following math problem using PyQBPP.

Math Problem: Find all three-digit odd integers whose product of digits is 252.

Let $x$, $y$, and $z$ be the hundreds, tens, and ones digits of the integer, respectively. More specifically:

  • $x$ is an integer in $[1, 9]$,
  • $y$ is an integer in $[0, 9]$,
  • $t$ is an integer in $[0, 4]$,
  • $z = 2t + 1$ (so $z$ is odd).

Then the value $v$ of the three-digit integer $xyz$ is

\[\begin{aligned} v&=100x+10y+z \end{aligned}\]

We find all solutions satisfying:

\[\begin{aligned} xyz &= 252 \end{aligned}\]

PyQBPP program

The following PyQBPP program finds all solutions:

import pyqbpp as qbpp

x = qbpp.var("x", between=(1, 9))
y = qbpp.var("y", between=(0, 9))
t = qbpp.var("t", between=(0, 4))
z = 2 * t + 1
v = x * 100 + y * 10 + z

f = (x * y * z == 252)

f.simplify_as_binary()
solver = qbpp.ExhaustiveSolver(f)
result = solver.search(best_energy_sols=0)
s = set()
for sol in result.sols:
    s.add(sol(v))
for val in sorted(s):
    print(val, end=" ")
print()

In this program, x, y, and t are defined as integer variables with the ranges above. Then z, v, and f are defined as expressions. We create an Exhaustive Solver instance for f and store all optimal solutions in result.sols.

Because x, y, and t are encoded by multiple binary variables, different binary assignments can represent the same integer values. As a result, the same digit triple (x,y,z) may appear multiple times in result.sols. Therefore, we use a Python built-in set named s to remove duplicates by collecting only the resulting integer values v.

The integers in s are printed as follows:

479 497 667 749 947

Back to top

Page last modified: 2026.05.21.