# No ordinary magic square part 2. How many solutions are there?

Same rules as last time except this time count or calculate the number of possible solutions!

No ordinary magic square

Place 1-9 in the squares below in a manner such that none of the columns, rows or long diagonals have the same sum:

• the other question is not too much old. It was only half an hour old. So you could just add this extra question in that. – manshu Jun 9 '16 at 9:35
• @manshu If I did that how would I credit the first person to answer? I already marked an answer to my first question as correct so I cant take that back retroactively and credit the correct response to someone who answers the next question can I? – Greg Hastings Jun 9 '16 at 9:40
• It seems reasonable to me for this to be a separate question. – Gareth McCaughan Jun 9 '16 at 9:43
• I agree it should be a separate question. Even though the questions are closely related, the methods one could potentially employ are quite different. – user21939 Jun 9 '16 at 9:53
• Yup. You solve the original one by playing with numbers until you make all the sums distinct. You solve this one by writing a computer program. (Of course you could solve the original one by writing a program too, but that feels like overkill.) – Gareth McCaughan Jun 9 '16 at 9:54

According to the simple-minded 7-line Python program I just wrote, there are exactly

24960 solutions.

There is at least a 25% chance that I didn't make any stupid blunders.

Here's the code:

n=0
for p in itertools.permutations([1,2,3,4,5,6,7,8,9]):
a,b,c,d,e,f,g,h,i = p
a,b,c,d,e,f,g,h = [a+b+c,d+e+f,g+h+i,a+d+g,b+e+h,c+f+i,a+e+i,c+e+g]
if a in (b,c,d,e,f,g,h) or b in (c,d,e,f,g,h) or c in (d,e,f,g,h): continue
if d in (e,f,g,h) or e==f or e==g or e==h or f==g or f==h or g==h: continue
n += 1


Why two separate lines for the inequality checks? Just because it looks better that way. Why change from x in y to x==... at the end? Out of a vague idea that it might be a bit faster.

It surprised me how quickly this ran -- but 9! isn't really all that large.

• May you post the code? 7-lines sounds quite interesting. – Inazuma Jun 9 '16 at 9:45
• @gareth I am new here so please advise me what to do. I took advice of another user to edit my first post instead of posting a 2nd question. Is there some way I can delete this puzzle and just leave the first? – Greg Hastings Jun 9 '16 at 9:45
• Same result but waaaay more lines ;) – Fabich Jun 9 '16 at 9:46
• I would suggest that you leave it as two questions. Perhaps put a link in the first question to this one. – Gareth McCaughan Jun 9 '16 at 9:52
• (But note that I am not an entirely uninterested party -- if you delete this question, I guess I lose the rep I gained from it.) – Gareth McCaughan Jun 9 '16 at 9:52

My Python code to iterate the solutions and their respective $8$ sums:

def noms():
sums = set()
for p in permutations(range(1,10)):
for e in [p[i:i+3] for i in range(0, 9, 3)]
+ [p[i:9:3] for i in range(3)]
+ [p[0:9:4], p[2:7:2]]:
s = sum(e)
if s in sums:
break
else:
yield p, sums
sums.clear()

Counting them:

>>> sum(1 for solution in noms())
24960

There are $8$ symmetrically isomorphic sets of $3120$ arrangements: $4$ rotations and $2$ lines of symmetry (reflection in one of the diagonals is the same as a rotation plus a reflection).

There are $1538$ different sets of sums distributed as:

Sums  Count  Total
496    8     3968
782   16    12512
102   24     2448
106   32     3392
10   40      400
36   48     1728
2   64      128
4   96      384
------------------
1538         24960


An example of one the $496$ sets of sums that are unique up to symmetry is: $\{9,11,12,13,15,16,19,23\}$
which is yielded by:
2  4  7
5  1  3
8  6  9

2  5  8
4  1  6
7  3  9

7  3  9
4  1  6
2  5  8

7  4  2
3  1  5
9  6  8

8  5  2
6  1  4
9  3  7

8  6  9
5  1  3
2  4  7

9  3  7
6  1  4
8  5  2

9  6  8
3  1  5
7  4  2

The smallest sum of sums is $107$, which necessarily has $1$ or $2$ in the centre) and may be made in $88$ ways, or $11$ up to symmetry ($3\times 1+4\times 2$):
1  7  4
9  2  8
3  6  5  {8, 9, 12, 13, 14, 15, 17, 19}
-----------------------------------------
1  6  5
8  2  9
4  7  3  {6, 11, 12, 13, 14, 15, 17, 19}
-----------------------------------------
2  5  4
8  1  7
3  9  6  {8, 9, 11, 13, 15, 16, 17, 18}
-----------------------------------------
1  7  4
8  2  6
5  9  3  {6, 11, 12, 13, 14, 16, 17, 18}

2  7  4
8  1  5
6  9  3  {6, 11, 12, 13, 14, 16, 17, 18}
-----------------------------------------
1  6  3
9  2  7
4  8  5  {8, 9, 10, 14, 15, 16, 17, 18}

2  5  3
9  1  7
4  8  6  {8, 9, 10, 14, 15, 16, 17, 18}
-----------------------------------------
1  6  3
8  2  9
4  7  5  {8, 9, 10, 13, 15, 16, 17, 19}

2  5  3
7  1  8
4  9  6  {8, 9, 10, 13, 15, 16, 17, 19}
-----------------------------------------
1  6  5
9  2  8
3  7  4  {7, 10, 12, 13, 14, 15, 17, 19}

2  5  6
7  1  9
3  8  4  {7, 10, 12, 13, 14, 15, 17, 19}
-----------------------------------------

The largest sum of sums is $133=8\times30-107$ and is the same set of squares where each of the $9$ digits, $d$, are replaced by $10-d$ and each of the $8$ sums, $s$, are replaced by $3*10-s$.

The lowest possible variance for the sums is $6.234375$, and there are $4\times 8=32$ such squares:
1  7  2
9  4  5
6  3  8  {10, 12, 13, 14, 15, 16, 17  18}

2  5  6
9  8  3
4  1  7  {10, 12, 13, 14, 15, 16, 17, 18}

3  7  4
9  2  5
6  1  8  {10, 12, 13, 14, 15, 16, 17, 18}

2  5  8
7  6  3
4  1  9  {12, 13, 14, 15, 16, 17, 18, 20}

The highest possible variance for the sums is $22.25$, and there are $4\times 8=32$ such squares:
1  2  3
4  6  8
5  7  9  {6, 10, 14, 15, 16, 18, 20, 21}

1  2  3
5  7  9
4  6  8  {6, 10, 14, 15, 16, 18, 20, 21}

1  2  7
3  4  8
5  6  9  {9, 10, 12, 14, 15, 16, 20, 24}

2  1  7
4  3  8
6  5  9  {9, 10, 12, 14, 15, 16, 20, 24}