# Ramanujan's method for solving a Magic Square

Ramanujan came up with a formula for solving a magic square.

Theorem 1: Let $m_1,m_2$ denote the sums of the middle row and middle column respectively for a $3\times 3$ square array of numbers. Let $c_1,c_2$ denote the sums of the main diagonal and secondary diagonal respectively. And lastly, let $S$ denote the sum of all nine elements of the square. If $x$ denotes the center element, then$$x=\frac 13\left(m_1+m_2+c_1+c_2-S\right)\tag1$$ Suppose that the sum of each row and column is equal to $r$. Then, we have$$x=\frac 13\left(c_1+c_2-r\right)\tag2$$ And note that that implies that $x=\frac r3$, so $r$ is a multiple of $3$. And lastly, in a $3\times 3$ magic square, the elements in the middle row, middle column and diagonals are in arithmetic progression. More clearly, let $a,b$ be the first and third elements respectively. Then$$a+r/3+b=r\tag3$$Hence$$b-r/3=r/3-a\tag4\\\vdots$$

Although the formula is clear, I'm just wondering how you would go about actually solving a magic square.

Question: Using Theorem 1, how would you solve a magic square? (Example provided below)

For example:

• Construct a magic square with $r=15$ and all numbers are odd!

My Attempt: Since $r=15$, we have $x=15/3=5$. Thus, the middle digit$^{[1]}$ is $5$. From $(3)$, we get a Diophantine equation, and solving we get $$(a,b)=(1,9)\\(a,b)=(3,7)\\(a,b)=(5,5)\tag5$$ After that, I'm not sure what to do. I have $3$ possible $a,b$ values and I'm not sure which two I should use. Any help would be accepted!

$\scriptsize{[1]:\text{Maybe..?}}$

• Magic squares contain distinct numbers. The matrix of all 5s would not be called a magic square. What your calculation shows is that either the middle row must be {3,5,7} and the middle column must be {1,5,9}, or vice versa. – user378953 Oct 28 '16 at 3:57
• @user378953 How would you then solve for the corner numbers with the proposed formula? – Frank Oct 28 '16 at 4:10
• Some educated guessing will take care of the rest, for example in the usual case with numbers $\{1,...,9\},$ we know $1$ has to be next to both $8$ and $6$ for the sum to be $15$. It isn't possible to have a magic square with $r=15$ and only odd numbers, unless you are including negatives in which case I'm not sure what to say. – user378953 Oct 28 '16 at 4:26

## 1 Answer

Why it's impossible:

There are actually 4 more arithmetic sequences that will always be present in a magic square. Let the square: $$a,b,c$$ $$d,e,f$$ $$g,h,i$$ So from $(3)$ and $e=\frac{r}{3}$ we get the equations: $$a-e=e-i$$ $$b-e=e-h$$ $$c-e=e-g$$ $$d-e=e-f$$ $(6)$ From these we get: $$2e=a+i=b+h=c+g=d+f$$ Next take: $$r=a+b+c=a+d+g\implies b+c=d+g$$ Then from $(6)$, substitute in $g=b+h-c$ to get: $$b+c=d+(b+h-c)$$ Rearrange: $$d-c=c-h$$ The same "trick" can be done too for $(b,g,f),(b,i,d),(h,a,f)$ either using the same method or the symmetries of the magic square.

$(7)$ We have: $$d-i=i-b$$ $$b-g=g-f$$ $$f-a=a-h$$ $$h-c=c-d$$ Note the middle elements are the corners, which is helpful for remembering these.

Back to your question now.

For $r=15$ we have 4 sequences across the center: $(1,9),(2,8),(3,7),(4,6)$

From these we can make 5 more arithmetic sequences: $(1,2,3),(2,4,6),(1,4,7),(3,6,9),(7,8,9)$

All 5 progressions have at least one even number so no such square is possible!

Finding the smallest odd magic square:

Since the square with $5$ in the center (and a total of $15$) is impossible. Let's try the square with $7$ in the center.

The sequences about 7 are $(1,13),(3,11),(5,9)$

From these we can pull the sequences $(1,3,5),(1,5,9),(9,11,13),(5,9,13)$

The middle elements will become the centers and the left/right elements will become the middles of the edges of the square. This implies $5$ and $9$ will be duplicated. So no distinct square exists with center element $e=7$

However for $e=9$ we can take the pairs about $9$: $(1,17),(3,15),(5,13),(7,11)$

Now note for the equations in $(7)$, each equation's last variable is the first variable of the equation following it. That is to say, we must pull 4 sequences from the elements of the pairs above such that they form a sort of loop with their first and last elements. The right sequence can be found with a small bit of work: $$\rightarrow(1,7,13)\rightarrow(13,15,17)\rightarrow(17,11,5)\rightarrow(5,3,1)\rightarrow$$

Now we construct the square, starting with $(1,7,13)$: $$a,[1],c$$ $$[13],[9],f$$ $$g,h,[7]$$ Then $(13,15,17)$: $$a,1,[15]$$ $$[13],9,f$$ $$g,[17],7$$ Then $(17,11,5)$: $$[11],1,15$$ $$13,9,[5]$$ $$g,[17],7$$ Finally $(5,3,1)$: $$11,[1],15$$ $$13,9,[5]$$ $$[3],17,7$$ And we're done. The smallest all odd magic square has $r=27$ $$11,1,15$$ $$13,9,5$$ $$3,17,7$$

• Er, he asked for a square with odds only? – Parcly Taxel Oct 28 '16 at 5:14
• @Christian Woll The solution given under the book is [![Solution][1]][1]Wonder why they included the restriction of all numbers odd... [1]: i.stack.imgur.com/bX9mV.png – Frank Oct 31 '16 at 21:27