# How to solve 3x3 Magic Squares with negative values when only 2 values are given?

I'm prepping for this math contest and I've been given notice that the special question is a magic square (this is Caribou Contest, they tell you on their website what the special question is a couple weeks prior so this should be legal). So naturally I decided to go take a look at what a magic square is. After a couple hours of solving a bunch them on the website I think I got a pretty good grasp on how to do them. However, when I tried to tackle the toughest difficulty setting on the website (Megaloceros) I was confronted by the problem: $$\begin{array}{|c|c|c|} \hline A & B & C \\ \hline -302 & D & E \\ \hline F & -128 & G\\ \hline \end{array}$$

I managed to find C by taking the average of -302 and -128 and got 215 so we have:

$$\begin{array}{|c|c|c|} \hline A & B & 215 \\ \hline -302 & D & E \\ \hline F & -128 & G\\ \hline \end{array}$$

Now that I got this, I was tempted to try solving this using a system of linear equations but I also wanted to find the "elegant" way to solve this.

Also if you know a general way to solve a magic square with negative numbers it would be greatly appreciated. I found on a similar question a general way to solve 3x3 magic squares if they had to be filled with the numbers {1...n^{2}}. However, this is not possible because there aren't enough spaces for consecutive numbers.

P.S. I don't know how to add spacing to make the magic squares columns align properly, sorry

P.P.S These magic squares have no limit to the size of the numbers, and they only use addition on the rows, columns, and diagonals

• This question appears to be fine; since the contest hasn't started, and you're just asking about general strategies for solving them. (Apologies for the rashness of the previous commenters -- we get a lot of questions from people who want us to do their homework or exams for them.)
– Deusovi
Oct 30 '20 at 3:19
• Incidentally, the average (arithmetic mean) of -302 and -128 is not 215, it's -215.
– JDL
Oct 30 '20 at 13:43

Turns out you don't have enough information -- but you could put anything you wanted in any other cell!

As shown by Joe Z in this answer, all 3×3 magic squares can be expressed as:

$$\begin{array}{|c|c|c|} \hline b & 2b+2c-3a & c \\ \hline 2c-a & b+c-a & 2b-a \\ \hline 2b+c-2a & a & b+2c-2a\\ \hline \end{array}$$ for some values of $$a$$, $$b$$, and $$c$$. (I've flipped his version around for clarity, but it's the same thing.)

So, you know $$a$$ and $$2c-a$$. With those values, you can find $$c$$ too. But that means that the top-right value is already determined! As you figured out, that corner must be the average of the two opposing sides: $$c = \frac{a + (2c-a)}2$$.

So, you could put anything you wanted into any of the other cells. Once you decide on a value, that would determine your $$b$$, and so you'd be able to find a magic square. But you don't have enough information to find the specific magic square they were thinking of.

you can make infinitely many different magic squares with only two numbers given as in the question, so you can't solve it (or you can pick one of the infinite possibilities).

Nevertheless, some knowledge about the properties of 3x3 magic squares can be helpful to tackle the general category of questions:

1. If you restrict the entries to distinct numbers from 1 to 9 (inclusive), exactly 8 magic squares exist, all of which are reflections and/or rotations of each other:

$$\begin{array}{|ccc|ccc|ccc|ccc|} \hline 8&1&6&6&7&2&2&9&4&4&3&8\\ 3&5&7&1&5&9&7&5&3&9&5&1\\ 4&9&2&8&3&4&6&1&8&2&7&6\\ \hline 6&1&8&2&7&6&4&9&2&8&3&4\\ 7&5&3&9&5&1&3&5&7&1&5&9\\ 2&9&4&4&3&8&8&1&6&6&7&2\\ \hline \end{array}$$

1. Without the restriction, there exists a general formula that generates every possible integer magic square: (Source: Wikipedia)

$$\begin{bmatrix} c-b & c+a+b & c-a \\ c-a+b & c & c+a-b \\ c+a & c-a-b & c+b \\ \end{bmatrix} \\= c\begin{bmatrix}1&1&1\\1&1&1\\1&1&1\\\end{bmatrix} +a\begin{bmatrix}0&1&-1\\-1&0&1\\1&-1&0\\\end{bmatrix} +b\begin{bmatrix}-1&1&0\\1&0&-1\\0&-1&1\\\end{bmatrix}$$

I think this formula is the simplest possible and quite easy to remember, if you want to use some formula in the contest.

Plugging your question into this matrix gives two equations $$c-a+b=-302$$ and $$c-a-b=-128$$. From that you can derive the values of $$c-a=-215$$ and $$b=-87$$, filling in the upper right corner as you already did (though you had a mistake on its sign). Now we have freedom to choose the values of $$c$$ and $$a$$, as long as they satisfy $$c-a=-215$$. Almost any choice will give you a valid magic square (excluding a very bad luck where you get duplicate numbers).

I think the simplest way to do this (knowing 2 or three fields), is to remember two magic squares with zeros $$\begin{bmatrix}1&0&2\\2&1&0\\0&2&1\\\end{bmatrix} \begin{bmatrix}0&3&0\\1&1&1\\2&-1&2\\\end{bmatrix}$$ The first one can be easily rotated to add two given non center values, e.g.: $$-302/2 \times \begin{bmatrix}0&2&1\\2&1&0\\1&0&2\\\end{bmatrix} -128/2 \times \begin{bmatrix}2&0&1\\0&1&2\\1&2&0\\\end{bmatrix}$$

Note: the trick is to rotate such that the other given-value's position is zero.

When possible, a third one can be 'easily' added afterwards, e.g.: $$-302/2 \times \begin{bmatrix}0&2&1\\2&1&0\\1&0&2\\\end{bmatrix} -128/2 \times \begin{bmatrix}2&0&1\\0&1&2\\1&2&0\\\end{bmatrix} + (D+(302-128)/2) \times \begin{bmatrix}1&2&0\\0&1&2\\2&0&1\\\end{bmatrix}$$ However, without sides (i.e. only corners/center) specified, we need the second square for an easy computation, e.g.: $$C \times \begin{bmatrix}0&2&1\\2&1&0\\1&0&2\\\end{bmatrix} +A \times \begin{bmatrix}1&2&0\\0&1&2\\2&0&1\\\end{bmatrix} + (G/2-A-C/2) \times \begin{bmatrix}0&3&0\\1&1&1\\2&-1&2\\\end{bmatrix}$$