Magic Square

Magic Square in a Magic Hat

A Magic Square is an n x n matrix where the numbers from 1 to n2 are arranged so that the sum of any row, column, or diagonal is the same, equal to n(n2 + 1) / 2. I originally wrote my Magic Square generator in Java, but when I revised this page in August 2007, I felt it would be better to convert it to a JavaScript application. I now generate squares for all values of n, instead of just the simple case where n is odd. I used the algorithms on Shin's Magic Square World as the basis for my implementation, since they work well and are described in detail. In general, there are three separate cases to consider when generating a Magic Square:

  • The odd number series (n = 3, 5, 7, ...)
    This algorithm is well-known.
  • A multiple-of-4 series (n = 4, 8, 12, ...)
    This solution is also fairly common.
  • The rest (n = 6, 10, 14, ...)
    Shin states that only a few Magic Squares of this series have been known until recently.
 
Status: 
 
Magic Sum: 
 
Elapsed Time: