frazer wrote:More interesting would be to insist that the magic squares didn't entirely lie within one 3x3 block, being spread over 2 or even 4 blocks. How many magic squares would it be possible to fit into a valid grid then?

You can fit 5 magic squares into the grid, but it has to be the same magic square in each case and subject to strict positional constraints (unless there's a hideous bug in my code ...). It actually works out quite nicely:

1. pick one of the eight 1-9 magic squares, call it M, and place it in the middle of the grid.

2. now place a copy of M in the top row band indented by 1 or 2 places from either the left or right side of the grid (4 choices). Place another copy of M in the bottom row band, idented by the *same* amount from the other (right or left) side of the grid.

3. place a copy of M in the lefthand column stack idented by 1 or 2 places from the 'free' end (top or bottom) of that stack (your only choice is the indentation level; not top/bottom). Finally, place your last copy of M in the righthand column stack indented by the *same* amount.

That's 8 x 4 x 2 = 64 choices. For example:

- Code: Select all
`...|..4|38.`

...|..9|51.

438|..2|76.

---+---+---

951|438|...

276|951|438

...|276|951

---+---+---

.43|8..|276

.95|1..|...

.27|6..|...

Now all you need to do is fill in the remaining cells. Turns out, for each of the 64 possible arrangements, there is always precisely one way of completing the grid. Here's the solution for the partial grid above:

- Code: Select all
`519|764|382`

762|389|514

438|512|769

---+---+---

951|438|627

276|951|438

384|276|951

---+---+---

143|895|276

695|127|843

827|643|195

This is fun ... beats Harry Potter any day