Placing numbers of 1-9 so that the six equations hold

73 Views Asked by At

Place the numbers 1 to 9 into the nine positions in such a way that, the 6 equations are valid. Each position must have a distinct value. Multiplication and division have priority over addition and subtraction.

GridOfEquations

Well, this problem can be solved with some brute-force and heuristics in a non-difficult way. But I am wondering, if this can somehow be solved by linear/integer/constraint programming? If yes, how should I model it?

I had some ideas to model, but it turned out not to be working since some equations had multiplication and division. How can those be linearized?

1

There are 1 best solutions below

0
On

You can formulate this (or any similar problem) as an integer program (IP) or a constraint program (CP), with the caveat that it will be a bit tedious. I'll describe the IP approach in somewhat general terms, starting with the assumption that the slots to be filled are numbered from 1 to 9 in raster scan order (1 being the top left slot). I'm going to assume that either your IP solver understands implication constraints (many do) or you know how to convert an implication constraint into linear constraints (a question frequently answered on OR Stack Exchange).

Begin with binary variables $x_{i,j}$ where $x_{i,j} = 1$ means slot $i$ is filled with value $j.$ Add the obvious constraints that every slot is filled with exactly one value and every value is used exactly once.

Now add general integer variables for the value of each cell. So $y_1 = x_{1,1} + 2x_{1,2} + \dots + 9x_{1, 9}$ and so on. Row/column equations that only involve addition and subtraction can be expressed as linear constraints on the $y$ variables.

Where multiplication or division occurs, introduce yet another strictly positive integer variable for the product/ratio. (I'll revisit ratios below, since there is a complicating issue.) Let's say we use $z$ for $y_4 / y_7.$ The first column constraint becomes $y_1 - z = 2.$ Defining $z$ in a linear way involves a gaggle of constraints (I did mention this would be tedious). Some of them rule out combinations of values that do not produce an integer ratio and some dictate the value of the ratio. Two of those constraints are as follows: $$x_{4,1} + x_{7,3} \le 1;$$ $$(x_{4,6} + x_{7,2} = 2) \implies z=3.$$ The first constraint says you cannot have both $y_4=1$ and $y_7=3$ because 1/3 is not an integer; the second says that if $y_4 = 6$ and $y_7 = 2$ then $y_4 / y_7 = 3.$ For products, you do not need constraints to eliminate pairs of operands, just implication constraints to defined the products (bearing in mind that the product may be as large as 72).

The complicating issue with ratios I alluded to does not occur in this puzzle but hypothetically could in another puzzle. If one of the equations reads $A / B \times C = \dots$ where $A, B, C$ are three cells in the same row or column, it is not necessarily the case that $A / B$ is an integer. An example could be $7/2\times 4.$ The trick there is to use one new integer variable $z$ for $A\times C$, write $z$ as a binary expansion using yet another collection of 0-1 variables, and then handle $z / B$ as above.