Skip to main content
4 of 4
edited tags
Wheat Wizard
  • 100k
  • 22
  • 282
  • 661

Generate all \$3\times 3\$ magic squares

Though challenges involving magic squares abound on this site, none I can find so far ask the golfer to print / output all normal magic squares of a certain size. To be clear, a normal magic square of order \$n\$ is:

  1. An \$n\times n\$ array of numbers.
  2. Each positive integer up to and including \$n^2\$ appears in exactly one position in the array, and
  3. Each row, column, and both main- and anti-diagonals of the array sum to the same "magic number".

Your challenge is to write the shortest program that prints all normal magic squares of order 3. There are 8 such squares. For the purposes of this challenge, the characters surrounding the output array, such as braces or brackets that accompany arrays in many languages, are allowed. For a non-optimized example in python, see here. Happy golfing!

rajb245
  • 243
  • 1
  • 7