![]() |
VOOZH | about |
A 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) / 2.
Examples:
Magic Square of order 3: ----------------------- 2 7 6 9 5 1 4 3 8 Magic Square of order 4: ----------------------- 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 Magic Square of order 8: ----------------------- 64 63 3 4 5 6 58 57 56 55 11 12 13 14 50 49 17 18 46 45 44 43 23 24 25 26 38 37 36 35 31 32 33 34 30 29 28 27 39 40 41 42 22 21 20 19 47 48 16 15 51 52 53 54 10 9 8 7 59 60 61 62 2 1
A bit of Theory:
Magic squares are divided into three major categories depending upon order of square.
1) Odd order Magic Square. Example: 3,5,7,... (2*n +1)
2) Doubly-even order Magic Square. Example : 4,8,12,16,.. (4*n)
3) Singly-even order Magic Square. Example : 6,10,14,18,..(4*n +2)
Algorithm for Doubly-even Magic Square :
define an 2-D array of order n*n
// fill array with their index-counting
// starting from 1
for ( i = 0; i<n; i++)
{
for ( j = 0; j<n; j++)
// filling array with its count value
// starting from 1;
arr[i][j] = (n*i) + j + 1;
}
// change value of Array elements
// at fix location as per rule
// (n*n+1)-arr[i][j]
// Top Left corner of Matrix
// (order (n/4)*(n/4))
for ( i = 0; i<n/4; i++)
{
for ( j = 0; j<n/4; j++)
arr[i][j] = (n*n + 1) - arr[i][j];
}
// Top Right corner of Matrix
// (order (n/4)*(n/4))
for ( i = 0; i< n/4; i++)
{
for ( j = 3* (n/4); j<n; j++)
arr[i][j] = (n*n + 1) - arr[i][j];
}
// Bottom Left corner of Matrix
// (order (n/4)*(n/4))
for ( i = 3* n/4; i<n; i++)
{
for ( j = 0; j<n/4; j++)
arr[i][j] = (n*n + 1) - arr[i][j];
}
// Bottom Right corner of Matrix
// (order (n/4)*(n/4))
for ( i = 3* n/4; i<n; i++)
{
for ( j = 3* n/4; j<n; j++)
arr[i][j] = (n*n + 1) - arr[i][j];
}
// Centre of Matrix (order (n/2)*(n/2))
for ( i = n/4; i<3* n/4; i++)
{
for ( j = n/4; j<3* n/4; j++)
arr[i][j] = (n*n + 1) - arr[i][j];
}
Explanation with an example:(order 4)
1) Define array of order 4*4 and fill it with its count value as:
2) Change value of top-left corner matrix of order (1*1):
3) Change value of top-right corner matrix of order (1*1):
4) Change value of bottom-left corner matrix of order (1*1):
5) Change value of bottom-right corner matrix of order (1*1):
6) Change value of centre matrix of order (2*2):
Implementation for Doubly-even Magic Square:
Output:
64 63 3 4 5 6 58 57 56 55 11 12 13 14 50 49 17 18 46 45 44 43 23 24 25 26 38 37 36 35 31 32 33 34 30 29 28 27 39 40 41 42 22 21 20 19 47 48 16 15 51 52 53 54 10 9 8 7 59 60 61 62 2 1
Time complexity : O(n2)
Auxiliary Space: O(n2). since n2 extra space has been taken.
Reference: https://www.1728.org/magicsq2.html