Magic Square Made Easy
Form magic square of any odd size with natural numbers
Supplies
Excel or Google Spreadsheet
Form a Square Matrix
Write numbers 1 to nxn in sequence. Here n is the order of the Magic Square, say 5. Name the matrix range as magic.
Row and Column Matrices
We are trying to identify the final Matrix from the above. Form two matrices by arranging numbers 1 to n as shown in the diagram. You will see the middle column of the left matrix starts with 1 and are in sequence. Right matrix is a mirror of the left matrix. Lets see how left matrix is used to identify the row of the final matrix and right one to identify the column in the next step.
Magic Square
Form the final Matrix by writing the number from the matrix formed in Step 1. Look for the corresponding row (green cell in row matrix) and column (green cell in column matrix) in the matrix formed in Step 1. For e.g 4, 3 (Step 2) = 18 (Step 1) and Sample 2 => 3, 1 (Step 2) = 11 (Step 1). The above steps are applicable for any odd size of Magic Square!
Java Program to Solve
/*
* Magic Square
*/
int order = 5;
for (int row = 0; row < order; row++) {
for (int col = 0; col < order; col++) {
int rowMatrix = (((order + 1) / 2 + row + col) % order);
int colMatrix = (((order + 1) / 2 + row + order - col - 1) %
order) + 1;
System.out.print(((rowMatrix * order) + colMatrix) + "\t");
}
System.out.println();
}