Skip to main content
1 of 4
rajb245
  • 243
  • 1
  • 7

Generate all \$n\times n\$ 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. 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