Solve 6 simultaneous equations for 8 variables puzzle

How to solve this puzzle? The image was sent to me with a caption in Chinese (解了一天了 帮帮忙吧… - googling leads to some solutions) and blank spaces where I have added letters.

Separating each row and column into individual equations gives the following 6 simultaneous equations:

$a + b - 9 = 4$

$c - de = 4$

$f + g - h = 4$

$a + \frac{c}{f} = 4$

$b - dg = 4$

$9 - e - h = 4$

I believe there must be many, probably infinite solutions, for the above system. But how would one go about mathematically finding one?

If I'm not mistaken, $-\frac{c}{f}-c+de+dg+e-f-g+2h-18=0$ is one general form of the above system. How would I use that to extract real (integer) solutions?

• Usually these kind of questions require that the nine digits (including the 9 given) be a permutation of 12...9. That makes the solution unique. Apr 18 '16 at 14:48
• In these puzzles the order of operations is not usually respected. The mathematically correct way to read the center line is the way you did, $c-de=4$, but in these puzzles it is usually $(c-d)e=4$. I would not be so sure that the digits are unique, but I would count on each variable being a single digit. Apr 18 '16 at 14:56
• I agree with Ross Millikan. I just ran a Python program trying to go through all of the digits 1-9 with your equations and none of them worked. With Ross Millikan's way, though, there is a solution with unique $a, b, c, d, e, f, g, h$. Apr 18 '16 at 15:00
• Oh wow. I might have to try again with this unusual order of operations. Apr 18 '16 at 15:19

I assume all the variables are single digits, but do not assume that each digit is represented once in the grid. It is clear that you are not supposed to respect the usual order of operations, because $a$ must be at least $5$, so if you read the first column $a+\frac cf=4$ there is no solution. It must be $\frac{a+c}f=4$

$e$ and $g$ must be $1,2,$ or $4$. If $e=1, h=4, g=2, f=6$ and the first column fails. If $e=2, h=3,$ and $g$ is too large for the first column. If $e=4, h=1, g=2, f=3,$ and $a+b=13, a+c=12$ This means $a \ge 5$. It cannot be $8$ because $c$ would be $4$. It can't be $6$ because $c$ would be as well. $a=7, c=5, d=4$ gives a solution with two fours. $a=5, c=7, b=8, d=6$ gives a solution with all digits unique.

Generally in problems like this I look for the cells that are most restricted and follow where the possibilities lead.

I have absolutely no idea how to solve this problem by hand. Here's the code of a Python program to solve the problem:

def calc(a, b, c, d, e, f, g, h):
'''
This function checks whether or not all six equations work.
If one of them fails, it says False.
If all of them work, it says True.
'''
if a+b-9 != 4: return False
if (c-d)*e != 4: return False
if f+g-h != 4: return False
if (a+c)/f != 4: return False
if (b-d)*g != 4: return False
if 9-e-h != 4: return False
return True
# Loop through all possible digits a, b, c, d, e, f, g, h.
for a in range(1, 9):
for b in range(1, 9):
for c in range(1, 9):
for d in range(1, 9):
for e in range(1, 9):
for f in range(1, 9):
for g in range(1, 9):
for h in range(1, 9):
# If the calculation works, then print the digits.
if calc(a, b, c, d, e, f, g, h):
print(a, b, c, d, e, f, g, h)

Here is the output of such program:

(5, 8, 7, 6, 4, 3, 2, 1)
(6, 7, 6, 5, 4, 3, 2, 1)
(6, 7, 8, 6, 2, 3, 4, 3)
(7, 6, 5, 4, 4, 3, 2, 1)
(7, 6, 7, 5, 2, 3, 4, 3)
(8, 5, 4, 3, 4, 3, 2, 1)
(8, 5, 6, 4, 2, 3, 4, 3)
(8, 5, 8, 4, 1, 4, 4, 4)

As you can see the first solution is the only solution with unique digits, so I think that is what was intended.

• Nice program!!! Apr 18 '16 at 15:11
• I submitted a Java version to codereview.SE Apr 19 '16 at 0:42
• But why you have only 8 answers? My program found 14. Apr 19 '16 at 16:07
• @IvanZelenskyy Did you go from just $1-9$? Also, can I see your code? Apr 19 '16 at 16:30
• @NobleMushtak, of course, if this puzzle has rules like 'magik square' (numbers 1-8 only without repeats) there only one solution: [5, 8, 7, 6, 4, 3, 2, 1] Apr 20 '16 at 7:43