_=>(h=S=>S[1]?S.flatMap(e=>h(S.filter(a=>a!=e)).map(a=>[e,...a])):[S])([1,2,3,4,5,6,7,8,9]).filter(e=>[[0,3],[0,1],[3,1],[6,1],[1,3],[2,3],[0,4],[2,2]].every(l=>e[l[0]]+e[l[0]+=l[1]]+e[l[0]+l[1]]==15))
Try it online!
Explanation
The integers 1 thru 9 should appear once each in the magic square, so the sum of all elements is \$\frac{9(9+1)}{2}=45\$ using a relatively simple formula.
Therefore, since each row forms one-third of the magic square, the sum of each row should equal 15 (and, by definition, the sums of columns and three-length diagonals must also equal 15).
The first thing my code does is that it creates all permutations of [1,2,3,4,5,6,7,8,9]
(362880 of them to be exact) and checks for the ones which follow specifications. This is done using a special procedure I used in the Parker square challenge.
Rows, columns and diagonals are encoded in a format [b, i]
:
b
is the starting index
i
is the number of indexes to add
The obtained subarray for such an array [b, i]
would be (if the original array was l
) l[b], l[b+i], l[b+i+i]
. This is done using a slightly golfier method.
We check if, for each array, the corresponding sub-array (obtained via simple indexing methods) has a sum of 15.
As it turns out there are only eight magic squares of order 3. Right after writing my solution (and golfing it), I returned to this question and clicked the math.se
link to find that someone had come up with a very simple formula. This notwithstanding, I am posting my answer because I think it offers a more creative approach than simple hard-coding. I am open to suggestions for reducing length.