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.between(qbpp.var_int("x"), 1, 9)
y = qbpp.between(qbpp.var_int("y"), 0, 9)
t = qbpp.between(qbpp.var_int("t"), 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})
results = set()
for sol in result.sols():
    results.add(sol(v))
for val in sorted(results):
    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 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 sols. Therefore, we use a set to remove duplicates by collecting only the resulting integer values v.

The integers are printed as follows:

479 497 667 749 947

3桁の数学問題

以下の数学問題をPyQBPPを用いて解きます。

数学問題各桁の積252である3桁の奇数をすべて求めてください。

$x$、$y$、$z$ をそれぞれ百の位、十の位、一の位の数字とします。 より具体的には:

  • $x$ は $[1, 9]$ の整数、
  • $y$ は $[0, 9]$ の整数、
  • $t$ は $[0, 4]$ の整数、
  • $z = 2t + 1$($z$ は奇数)。

3桁の整数 $xyz$ の値 $v$ は

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

以下を満たすすべての解を求めます:

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

PyQBPPプログラム

以下のPyQBPPプログラムですべての解を求めます:

import pyqbpp as qbpp

x = qbpp.between(qbpp.var_int("x"), 1, 9)
y = qbpp.between(qbpp.var_int("y"), 0, 9)
t = qbpp.between(qbpp.var_int("t"), 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})
results = set()
for sol in result.sols():
    results.add(sol(v))
for val in sorted(results):
    print(val, end=" ")
print()

このプログラムでは、xyt を上記の範囲の整数変数として定義します。 次に zvf を式として定義します。 f に対するExhaustive Solverインスタンスを作成し、すべての最適解を sols に格納します。

xyt は複数のバイナリ変数でエンコードされるため、異なるバイナリ割り当てが同じ整数値を表す場合があります。 その結果、同じ数字の組 (x,y,z) が sols に複数回現れる可能性があります。 そのため、結果の整数値 v のみを集めることで set を使って重複を除去しています。

出力される整数は以下の通りです:

479 497 667 749 947