Same rules as last time except this time count or calculate the number of possible solutions!
Place 1-9 in the squares below in a manner such that none of the columns, rows or long diagonals have the same sum:
Puzzling Stack Exchange is a question and answer site for those who create, solve, and study puzzles. It only takes a minute to sign up.
Sign up to join this communitySame rules as last time except this time count or calculate the number of possible solutions!
Place 1-9 in the squares below in a manner such that none of the columns, rows or long diagonals have the same sum:
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.
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 sums.add(s) else: yield p, sums sums.clear()
Counting them:
>>> sum(1 for solution in noms()) 24960
Some more information:
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}