Vietnamese math puzzle

Alex Bellos shared the following math puzzle that was given to Vietnamese eight-year-olds. The challenge is to place the digits from 1 to 9 into the grid.

Vietnamese math puzzle

Preliminary remarks

  1. Although it wasn’t stated explicitly, I assume that each digit should be used only once.
  2. The colon signifies division.
  3. There are many correct answers. I assume that students were meant to use enlightened trial and error to find a solution, but were not expected to find all solutions.
  4. I am assuming the standard order of operations, but I am not sure if this was the original intention. Would an eight-year-old be expected to understand that multiplication and division have higher precedence than addition and subtraction?
  5. Frankly, this puzzle is kind of boring for grown-ups.

Python code

Searching for solutions by hand did not appeal to me, so I wrote a Python program to search for all solutions. I hope that this example is helpful for people who are learning Python. (Perhaps it will inspire someone to write a better program than this.)

from itertools import permutations
standard_order = True
tolerance = 1e-7
target = 66

if standard_order:
    expr = "? + 13 * ? / ? + ? + 12 * ? - ? - 11 + ? * ? / ? - 10"
else:
    expr = "(((? + 13) * ? / ? + ? + 12) * ? - ? - 11 + ?) * ? / ? - 10"
expr = expr.replace("?", "%d")

for inputs in permutations(range(1,10)):
    expression = expr % inputs
    result = eval(expression)
    if abs(result - target) < tolerance:
        print ("%s = %s" % (expression, target))

Discussion

The itertools package provides many useful tools for iteration, including the permutation function, which generates all permutations of a list. Our script iterates through all 362880 permutations of the set of integers from 1 to 9.

Each permutation is substituted into the expression and evaluated. The program uses the eval function, which allows Python to run Python code within itself. This can be very dangerous, so use this function with care.

The division operations can lead to round-off error, so we only check that the result is very close to the target value. The variable tolerance sets the required precision.

Output

There are 136 solutions.

1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 3 * 5 / 9 - 10 = 66
1 + 13 * 2 / 6 + 4 + 12 * 7 - 8 - 11 + 5 * 3 / 9 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 7 * 9 / 6 - 10 = 66
1 + 13 * 3 / 2 + 4 + 12 * 5 - 8 - 11 + 9 * 7 / 6 - 10 = 66
1 + 13 * 3 / 2 + 9 + 12 * 5 - 6 - 11 + 4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 - 5 - 11 + 2 * 3 / 8 - 10 = 66
9 + 13 * 1 / 4 + 7 + 12 * 6 - 5 - 11 + 3 * 2 / 8 - 10 = 66
9 + 13 * 2 / 8 + 7 + 12 * 6 - 5 - 11 + 1 * 3 / 4 - 10 = 66
9 + 13 * 2 / 8 + 7 + 12 * 6 - 5 - 11 + 3 * 1 / 4 - 10 = 66
9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 7 * 8 / 4 - 10 = 66
9 + 13 * 3 / 1 + 6 + 12 * 2 - 5 - 11 + 8 * 7 / 4 - 10 = 66
9 + 13 * 3 / 2 + 1 + 12 * 5 - 6 - 11 + 4 * 7 / 8 - 10 = 66
9 + 13 * 3 / 2 + 1 + 12 * 5 - 6 - 11 + 7 * 4 / 8 - 10 = 66
9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 3 * 8 / 6 - 10 = 66
9 + 13 * 4 / 1 + 5 + 12 * 2 - 7 - 11 + 8 * 3 / 6 - 10 = 66
9 + 13 * 4 / 8 + 5 + 12 * 6 - 7 - 11 + 1 * 3 / 2 - 10 = 66
9 + 13 * 4 / 8 + 5 + 12 * 6 - 7 - 11 + 3 * 1 / 2 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 7 * 8 / 6 - 10 = 66
9 + 13 * 5 / 3 + 1 + 12 * 4 - 2 - 11 + 8 * 7 / 6 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 1 * 7 / 2 - 10 = 66
9 + 13 * 6 / 4 + 3 + 12 * 5 - 8 - 11 + 7 * 1 / 2 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 5 * 7 / 3 - 10 = 66
9 + 13 * 8 / 6 + 2 + 12 * 4 - 1 - 11 + 7 * 5 / 3 - 10 = 66

Leave a Reply

Your email address will not be published. Required fields are marked *