Magic Square

Magic SquareA magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2.The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the following value:
M = n(n^2+1)/2For normal magic squares of order n = 3, 4, 5, …, the magic constants are: 15, 34, 65, 111, 175, 260, …In this post, we will discuss how programmatically we can generate a magic square of size n. Before we go further, consider the below examples:Magic Square of size 32 7 69 5 14 3 8Sum in each row & each column = 3*(3^2+1)/2 = 15Magic Square of size 59 3 22 16 152 21 20 14 825 19 13 7 118 12 6 5 2411 10 4 23 17Sum in each row & each column = 5*(5^2+1)/2 = 65A method for constructing magic squares of odd order was published by the French diplomat de la Loubère in his book, A new historical relation of the kingdom of Siam (Du Royaume de Siam, 1693), in the chapter entitled The problem of the magical square according to the Indians. (Source: http://en.wikipedia.org/wiki/Magic_square#Method_for_constructing_a_magic_square_of_odd_order) The method operates as follows:Magic squareThe method prescribes starting in the central column of the first row with the number 1. After that, the fundamental movement for filling the squares is diagonally up and right, one step at a time. If a filled square is encountered, one moves vertically down one square instead, then continues as before. When an “up and to the right” move would leave the square, it is wrapped around to the last row or first column, respectively.Let us understand the above procedure of making a magic square with the help of an example. Following is the step-wise illustration of making a magic square of 5 x 5.Step 1_ _ 1 _ __ _ _ __ _ _ __ _ _ __ _ _ _Step 2_ _ 1 _ __ _ _ __ _ _ __ _ _ __ _ 2 _Step 3_ _ 1 _ __ _ _ __ _ _ __ _ _ 3_ _ 2 _Step 4_ _ 1 _ __ _ _ _4 _ _ _ __ _ _ 3_ _ _ 2 _Step 5_ _ 1 _ _5 _ _ _4 _ _ _ __ _ _ 3_ _ _ 2 _Step 6_ _ 1 _ _5 _ _ _4 6 _ _ __ _ _ 3_ _ _ 2 _Step 7_ _ 1 _ _5 7 _ _4 6 _ _ __ _ _ 3_ _ _ 2 _Step 8_ _ 1 8 _5 7 _ _4 6 _ _ __ _ _ 3_ _ _ 2 _Step 9_ _ 1 8 _5 7 _ _4 6 _ _ __ _ _ 3_ _ _ 2 9Step 10_ _ 1 8 _5 7 _ _4 6 _ _ _10 _ _ _ 3_ _ 2 9Step 11_ _ 1 8 __ 5 7 _ _4 6 _ _ _10 _ _ _ 311 _ _ 2 9Step 12_ _ 1 8 __ 5 7 _ _4 6 _ _ _10 12 _ _ 311 _ _ 2 9Step 13_ _ 1 8 __ 5 7 _ _4 6 13 _ _10 12 _ _ 311 _ _ 2 9Step 14_ _ 1 8 __ 5 7 14 _4 6 13 _ _10 12 _ _ 311 _ _ 2 9Step 15_ _ 1 8 15_ 5 7 14 _4 6 13 _ _10 12 _ _ 311 _ _ 2 9Step 16_ _ 1 8 15_ 5 7 14 164 6 13 _ _10 12 _ _ 311 _ _ 2 9Step 1717 _ 1 8 15_ 5 7 14 164 6 13 _ _10 12 _ _ 311 _ _ 2 9Step 1817 _ 1 8 15_ 5 7 14 164 6 13 _ _10 12 _ _ 311 18 _ 2 9Step 1917 _ 1 8 15_ 5 7 14 164 6 13 _ _10 12 19 _ 311 18 _ 2 9Step 2017 _ 1 8 15_ 5 7 14 164 6 13 20 _10 12 19 _ 311 18 _ 2 9Step 2117 _ 1 8 15_ 5 7 14 164 6 13 20 _10 12 19 21 311 18 _ 2 9Step 2217 _ 1 8 15_ 5 7 14 164 6 13 20 2210 12 19 21 311 18 _ 2 9Step 2317 _ 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 _ 2 9Step 2417 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 _ 2 9Step 2517 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9The Java program for creating a magic square of n * n, where n is odd, is as follows:class MagicSquare{public static void generateMagicSquare( int n ){

int magic[][] = new int[ n ][ n ];

int row = 0, col = n / 2, i, j, square = n * n;

for( i=1; i <= square ; i++ ){

magic[ row ][ col ] = i;
if( i % n == 0 ) row++;

else{

if( row == 0 ) row = n – 1;

else row–;

if( col == ( n – 1 ) ) col = 0;

else col++;

}

}
for( i = 0; i < n ; i++ ){

for( j = 0; j < n; j++ ){

System.out.print( magic[ i ][ j ] + “t” );

}

System.out.println();
}
}

public static void main(String args[]){

generateMagicSquare(7);

}}
The loop at line number 5 would run n*n times and it’s purpose is to control the number to be filled in the array. Line number 6 assigns the current value stored in i to the element position row and col. The remaining portion of the loop is for computing the next position where the next number is to be placed. The if-statement on line number 7, is for moving one row below when the next element which diagonally up towards the right is already filled up (see the step 5 and step 6 in the example of 5 x 5 magic square above, the same applies for step 10-11 and so on). The if-statement on line number 11, is for wrapping the row variable to the bottom once it reaches the top row. The if-statement on line number 13, is for wrapping the column to the first column once it reaches the last column. The second for loop is for displaying the contents of the array magic which is storing the magic square we have created.The output of the above program for generating a magic square of nxn, where n is odd (in this case 7) is as follows:30 39 48 1 10 19 2838 47 7 9 18 27 2946 6 8 17 26 35 375 14 16 25 34 36 4513 15 24 33 42 44 421 23 32 41 43 3 1222 31 40 49 2 11 20

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *