5
$\begingroup$

I have this puzzle and I want to solve by code. I wrote simple code trying to brute force but it fail in one condition

https://dotnetfiddle.net/cJyu3w

enter image description here

Anyone know of a link or source how to solve such problem?

Thanks

$\endgroup$
  • $\begingroup$ Try this. $\endgroup$ – Jens Mar 24 at 2:04
  • 1
    $\begingroup$ This one has no solution. $\endgroup$ – Daniel Mathias Mar 24 at 2:17
  • 2
    $\begingroup$ @DanielMathias Or, at least, there is no integer solution. $\endgroup$ – rhsquared Mar 24 at 2:45
  • 3
    $\begingroup$ a= 3.5 , b= 4.5, c= 9.5, d= 3.5 but I guess that's not what you want. $\endgroup$ – rhsquared Mar 24 at 2:46
  • 3
    $\begingroup$ ^ this is the only solution tho: see the columns, we know the sum of 4 numbers is 13+8=21. As the sum of first row is 8, the sum of second row is 13. Solving x+y=13, x-y=6 leads only one single solution. $\endgroup$ – athin Mar 24 at 5:54
14
$\begingroup$

First of all, let's see why your brute-forcing fails. (This is the puzzle part, the rest is plain old math.)

Let's check the parity of the answer numbers, by choosing an odd number (first picture) or an even number (second picture) as the top left unknown:

enter image description here enter image description here

No matter which you chose, the number at the bottom right would have to be both odd and even at the same time, so there's no integer solution.


However, there are four equations and four unknowns, so we should have at least one solution (unless the system is inconsistent), or even many, if the equations aren't independent.

Since you wanted to know how to solve this by a computer program, let's do this with Octave.

(Sometimes it's difficult to choose the best programming language for a given problem. Here, we have a free language that actually gives the solution to our problem as the first syntax example on its home page. Choices don't really come easier than that.)

To get started, let's write the four equations out. Let's name the unknowns with letters a-d, so we get:

  a +b       = 8
        c -d = 6
  a    +c    = 13
     b    +d = 8

Notice that I've placed all the letters in their own columns. This is important for the next step, where we turn this into a matrix of coefficients. That is, we only write the multipliers (0, 1 and -1 in this case) into the columns, and put some brackets around the stuff:

$$\begin{bmatrix} 1 & 1 & 0 & 0 \\ 0 & 0 & 1 & -1 \\ 1 & 0 & 1 & 0\\ 0 & 1 & 0 & 1 \end{bmatrix} \begin{bmatrix} a\\ b\\ c\\ d \end{bmatrix} = \begin{bmatrix} 8\\ 6\\ 13\\ 8 \end{bmatrix} $$

(The column with the letters is there just to keep the notation more or less mathematically valid.)

Then, we just type these in and let Octave solve it (using the backslash operator (A\b)):

A=[1 1 0 0
   0 0 1 -1 
   1 0 1 0
   0 1 0 1]

b=[8
   6
   13
   8]

A\b

(You can Try it online!)

This gives us the answer for all the unknowns (or a warning about a singular matrix if our equations weren't independent, or if our system was inconsistent):

   3.5000
   4.5000
   9.5000
   3.5000

Since we got a neat result without any warnings, we can just type it in the proper boxes to achieve the final result:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks that a well explanation. $\endgroup$ – Amjad Mar 24 at 14:55
  • 2
    $\begingroup$ bass has an excellent answer, but missed a quick simplification. The top row a + b = 8 and the right column b + d = 8 share a common addend and result. therefore, a = d. for a generalized solution, this might not be helpful, but for the question presented, it simplifies the problem. $\endgroup$ – Jim Mar 24 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.