A magic square of order n is an arrangement of n2 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 n2. The constant sum in every row, column and diagonal are 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(n2+1)/2
For normal magic squares of order n = 3, 4, 5, ..., the magic constants are: 15, 34, 65, 111, 175, 260, ...
Here we will discuss how programmatically we can generate a magic square of size n. Before we go further, consider the below examples:
2 7 6 9 5 1 4 3 8 Sum in each row & each column = 3*(32+1)/2 = 15
9 3 22 16 15 2 21 20 14 8 25 19 13 7 1 18 12 6 5 24 11 10 4 23 17 Sum in each row & each column = 5*(52+1)/2 = 65
20 12 4 45 37 29 28 11 3 44 36 35 27 19 2 43 42 34 26 18 10 49 41 33 25 17 9 1 40 32 24 16 8 7 48 31 23 15 14 6 47 39 22 21 13 5 46 38 30 Sum in each row & each column = 7*(72+1)/2 = 175
Did you find any pattern in which the numbers are stored?
In any magic square, the first number i.e. 1 is stored at position (n/2, n-1). Let this position be (i,j). The next number is stored at position (i-1, j+1) where we can consider each row & column as circular array i.e. they wrap around.
Three conditions hold:
- The position of next number is calculated by decrementing row number of the previous number by 1, and incrementing the column number of the previous number by 1. At any time, if the calculated row position becomes -1, it will wrap around to n-1. Similarly, if the calculated column position becomes n, it will wrap around to 0.
- If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1.
- If the calculated row position is -1 & calculated column position is n, the new position would be: (0, n-2).
2 7 6 9 5 1 4 3 8
Steps:
- position of number 1 = (3/2, 3-1) = (1, 2)
- position of number 2 = (1-1, 2+1) = (0, 0)
- position of number 3 = (0-1, 0+1) = (3-1, 1) = (2, 1)
- position of number 4 = (2-1, 1+1) = (1, 2) Since, at this position, 1 is there. So, apply condition 2. new position=(1+1,2-2)=(2,0)
- position of number 5=(2-1,0+1)=(1,1)
- position of number 6=(1-1,1+1)=(0,2)
- position of number 7 = (0-1, 2+1) = (-1,3) // this is tricky, see condition 3 new position = (0, 3-2) = (0,1)
- position of number 8=(0-1,1+1)=(-1,2)=(2,2) //wrap around
- position of number 9=(2-1,2+1)=(1,3)=(1,0) //wrap around