Magic Squares

In his autobiography, Benjamin Franklin wrote:
In my younger days, having once more leisure time (which I still think I might have employed more usefully), I had amused myself in making these kind of magic squares, and, at length acquired such a knack at it, that I could fill the cells of any magic square of reasonable size with a series of numbers as fast as I could write them ...
The following array is classified as a normal symmetrical pandiagonal magic square of odd order.
1018 11422
1124 720 3
17 51321 9
23 619 215
41225 816
It is a ``magic'' square because all rows and columns sum to the same number, 65. It is ``pandiagonal'' because not only do both principal diagonals sum to 65, but so do all five-element broken diagonals consisting of the m elements on any short diagonal and the 5 - m elements on the corresponding parallel short diagonal. The array is ``symmetrical'' since any two elements symmetrically placed about the center sum to n2 + 1 (26 in this case), where n is the length of a side. The square is said to be ``normal'' because its elements consist of the integers 1 to n2, each used exactly once. Finally, the square is obviously ``of odd order'' since n = 5. The magic sum for such squares will always be n(n2+1)/2, or, as noted, 65 for n = 5. The reason Franklin was able to generate such squares so easily is that some kinds can be constructed algorithmically; trial and error is not necessary. The example given was made by following a generalized version of an algorithm for constructing odd-order squares that was developed by De la Loubere, Louis XIV's envoy to Siam from 1687 to 1688:
  1. Start by placing the integer 1 in any square.
  2. Move one cell to the right and two cells up and place 2 in that cell; continue moving in that fashion and placing successive integers. If a move to the right or up takes you outside the square, consider the square to be cyclic in space and re-enter at the appropriate point.
  3. If the target cell is already occupied, place the next number just below the last one that was successfully placed, and continue as in step 2.
  4. Continue steps 2 and 3 until the square is filled.
(Actually, De la Loubere's original algorithm called for moving up only one cell, but it has been found that moving like a knight in chess generates pandiagonal magic squares rather than squares that are merely magic on rows, columns, and principal diagonals.)

To see a magic square constructed, provide input for each of the following:

Parameter Description
n The dimension of the magic square which must be odd. In the example above, n was entered as 5
I The row position where the starting value 1 is to be placed. The top row is row zero, the second row is row one, etc. In the example above, I was entered as 0; however, values of 5, 10, 500, -15, etc. would have worked equally well since the square is considered cyclic in space.
J The column position where the starting value 1 is to be placed. The leftmost column is column zero, the second column is column one, etc. In the example above, J was entered as 2; however, values of 7, 12, 502, -3, etc. would have worked equally well since the square is considered cyclic in space.
A When successive integers are being placed in the magic square, the normal move is A units to the right. In the example above, A was entered as 1; however, values of 6, 11, -4, etc. would have worked equally well since the square is considered cyclic in space.
B When successive integers are being placed in the magic square, the normal move is B units up. In the example above, B was entered as 2; however, values of 7, 12, -3, etc. would have worked equally well since the square is considered cyclic in space.
C If the normal move leads to an occupied cell, the alternative move is C units to the right. In the example above, C was entered as 0; however, values of 5, 15, -5, etc. would have worked equally well since the square is considered cyclic in space.
D If the normal move leads to an occupied cell, the alternative move is D units up. In the example above, D was entered as -1; however, values of 4, 9, -6, etc. would have worked equally well since the square is considered cyclic in space.