# Magic square with not distinct numbers

There's a 4x4 magic square:

4 0 1 0
3 0 2 0
0 3 0 2
0 4 0 1


Where 0s are different numbers, 1=1, 2=2, 3=3, 4=4. Only the rows and the columns have the same sum, the diagonals don't.

Question: If rotating or reflecting the magic square counts as a different solution, how many different magic squares is it possible to build?

I suppose if all the numbers were different, the solution would be 2 * 4! * 4!. But I just can't figure it out how to deal with the repeated numbers.

Let's start by giving names to the unknown quantities $$\begin{bmatrix} 4 & a & 1 & e \\ 3 & b & 2 & f \\ c & 3 & g & 2 \\ d & 4 & h & 1 \end{bmatrix}$$ and call the total sum of the rows and columns $T$. We can now subtract the 1st row from the 2nd column as follows $$(a+b+3+4)-(4+a+1+e)=T-T=0$$ $$|| \quad\quad\quad\quad\quad\quad$$ $$\quad\quad\quad\ \ \ b-e+2 \quad\quad\Rightarrow\quad\quad e=b+2$$

That column and row both contained $a$. This trick works whenever you choose a row and column that contain the same variable. When we do the same trick for the other variables, we get 3 more equalities for 4 total:

$$e=b+2 \quad\quad f=a+2 \quad\quad g=d+2 \quad\quad h=c+2$$

Substituting these into the square we get

$$\begin{bmatrix} 4 & a & 1 & b+2 \\ 3 & b & 2 & a+2 \\ c & 3 & d+2 & 2 \\ d & 4 & c+2 & 1 \end{bmatrix}$$

Most importantly, for some particular choice of $a,b,c,$ and $d$ this magic square is valid exactly when $$a+b=c+d$$ So if you're counting how many valid magic squares of this form are possible, there are $\bf{infinitely\ many}$. For example, choose $a=7$, $b=8$, $c=9$, $d=6$ and a magic square pops out

$$\begin{bmatrix} 4 & 7 & 1 & 10 \\ 3 & 8 & 2 & 9 \\ 9 & 3 & 8 & 2 \\ 6 & 4 & 11 & 1 \end{bmatrix}$$ If however you wanted to know, for some particular choice of $a,b,c$, and $d$, how many distinct squares can be made by rotations and reflections, then the answer is $\textbf{8}$. This is because there are 4 rotations and their corresponding reflections. (Note: a reflection of a reflection creates either the original square, or a rotation of the original square)

$\quad$ And finally if you wanted to also count exchanging rows and columns as part of "building" new squares, then their are two choices of $a,b,c$, and $d$ that affect the answer. Specifically $$(1)\quad\quad a=1 \quad b=2 \quad c=1 \quad d=2 \quad\quad\quad$$ $$(2)\quad\quad a=2 \quad b=1 \quad c=2 \quad d=1 \quad\quad\quad$$ With either of these two choices, two of the columns will be equal. (It's impossible for two rows to ever be equal, so we don't need to worry about that).

In conclusion, for most choices of $a,b,c,$ and $d$ are $8\cdot 4! \cdot 4! = 4608$ distinct squares.

Except when the choice of $a,b,c,$ and $d$ is $(1)$ or $(2)$, in which case there are $8 \cdot \frac{4!}{2} \cdot 4! = 2304$ distinct squares.