The consistent total in every row, column, and diagonal of a magic square is known as the magic constant, denoted as M. The magic constant for a standard magic square, solely determined by the value of n, is calculated as follows:
M = n(n2+1)/2
Let's explore the standard magic squares with dimensions n = 6, 7, 8, and beyond.
For these specific requests, the special numerical values are roughly: 372, 509, 660, 825, ...
Magic Square of size 4
1 6 11 16
12 7 2 17
8 13 18 3
4 9 14 19
Sum in each row & each column = 4*(34+1)/2 = 70
Magic Square of size 6
35 1 6 26 19 24
3 32 7 21 23 25
31 9 2 22 27 20
12 28 33 8 17 14
10 11 36 15 13 30
29 18 34 5 16 4
Sum in each row & each column = 6*(111+1)/2 = 666
Magic Square of size 8
60 37 14 53 30 7 46 23
39 16 57 34 11 50 27 4
18 61 38 15 54 31 8 47
62 40 17 56 33 10 49 26
41 19 63 35 12 55 32 9
20 58 36 13 52 29 6 45
59 21 42 64 25 2 44 28
22 43 65 24 1 48 15 51
Sum in each row & each column = 8*(260+1)/2 = 2080
The initial integer, 1, remains fixed at the location (n/2, n-1) within every magic square. Assume these coordinates as (i,j). Subsequently, the next integer is positioned at (i-1, j+1), considering each row and column as circular arrays that loop around.
Three requirements are accomplished:
The next number's placement is established by reducing the row number of the previous number by 1 and incrementing the column number of the previous number by 1. The projected row location will consistently loop back to n-1 if it becomes negative. The computed column location will similarly loop back to zero if it reaches n.
Example:
Magic Square of size 4
1 15 14 4
12 6 7 9
8 10 11 5
13 3 2 16
Steps:
- position of number 1 = (4/2, 4-1) = (2, 3)
- position of number 2 = (2-1, 3+1) = (1, 4)
- position of number 3 = (1-1, 4+1) = (0, 5)
- position of number 4 = (0-1, 5+1) = (-1, 6)
- new position = (0, 6-4) = (0, 2)
- position of number 5 = (0-1, 2+1) = (-1, 3)
- new position = (0, 3-2) = (0, 1)
- position of number 6 = (0-1, 1+1) = (-1, 2)
- new position = (0, 2-2) = (0, 0)
- position of number 7 = (0-1, 0+1) = (-1, 1)
- new position = (0, 1-2) = (0, -1)
- new position = (1, -1+4) = (1, 3)
- position of number 8 = (1-1, 3+1) = (0, 4)
- position of number 9 = (0-1, 4+1) = (-1, 5)
- new position = (0, 5-4) = (0, 1)
- position of number 10 = (0-1, 1+1) = (-1, 2)
- new position = (0, 2-2) = (0, 0)
- position of number 11 = (0-1, 0+1) = (-1, 1)
- new position = (0, 1-2) = (0, -1)
- new position = (1, -1+4) = (1, 3)
- position of number 12 = (1-1, 3+1) = (0, 4)
- position of number 13 = (0-1, 4+1) = (-1, 5)
- new position = (0, 5-4) = (0, 1)
- position of number 14 = (0-1, 1+1) = (-1, 2)
- new position = (0, 2-2) = (0, 0)
- position of number 15 = (0-1, 0+1) = (-1, 1)
- new position = (0, 1-2) = (0, -1)
- new position = (1, -1+4) = (1, 3)
- position of number 16 = (1-1, 3+1) = (0, 4)
Example:
Let's explore a program to comprehend the functionality of the magic square function in the C programming language.
#include <stdio.h>
#include <string.h>
void generateSquare(int n)
{
int magicSquare[n][n];
memset(magicSquare, 0, sizeof(magicSquare));
int i = n / 2;
int j = n - 1;
for (int num = 1; num<= n * n;) {
if (i == -1 && j == n) // 3rd condition
{
j = n - 2;
i = 0;
}
else {
if (j == n)
j = 0;
// 1st condition helper if next number
// is goes to out of square's upper side
if (i< 0)
i = n - 1;
}
if (magicSquare[i][j]) // 2nd condition
{
j -= 2;
i++;
continue;
}
else
magicSquare[i][j] = num++; // set number
j++;
i--; // 1st condition
}
// Print magic square
printf("The Magic Square for n=%d:\nSum of "
"each row or column %d:\n\n",
n, n * (n * n + 1) / 2);
for (i = 0; i< n; i++) {
for (j = 0; j < n; j++)
printf("%3d ", magicSquare[i][j]);
printf("\n");
}
}
// Driver program to test above function
int main()
{
int n = 5; // Works only when n is odd
generateSquare(n);
return 0;
}
Output:
The Magic Square for n=5:
Sum of each row or column 65:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Complexities:
Time Complexity: O(n2)
Auxiliary Space: O(n2)
NOTE: This method only functions for odd values of nWork just with n's ODD values.
Explanation:
- Start at the square's middle cell in the final column .
- Put the number 1
- Go to the cell in the top-right corner .
- If it is not within square bounds , use modulo with n.
- Go to the cell that follows taking the modulo .
- Move to the left cell once we have reached a multiple of n .
- If it is not within square bounds , use modulo with n.
- Put the following number, and then repeat the process.
Example:
Let's explore another example to grasp the functionality of the magic square function in the C programming language.
#include <stdio.h>
int main() {
int n = 5;
int magicSquare[n][n];
for (int i = 0; i< n; i++) {
for (int j = 0; j < n; j++) {
magicSquare[i][j] = -1;
}
}
// Indices for element to be inserted
int x = (n / 2), y = n - 1;
// Loop till n ^ 2 times
for (int i = 1; i<= n * n; i++) {
// Put the current element at (x, y)
magicSquare[x][y] = i;
if (i % n == 0) {
// If we get multiple of n, move left
y--;
} else {
// Else, move top-right
x--; y++;
}
// Take modulo to avoid out of bounds
x += n; x %= n;
y += n; y %= n;
}
// Print magic square
for (int i = 0; i< n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d ", magicSquare[i][j]);
}
printf("\n");
}
return 0;
}
Output:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Conclusion:
In summary, magic squares represent distinctive arrangements of numbers with the exceptional property of equal sums across rows, columns, and diagonals. Over the ages, they have captivated mathematicians, creators, and communities alike. Magic squares present a fascinating intersection of mathematics with various disciplines, showcasing both their mathematical intricacy and artistic allure, as well as their cultural significance and utility in encryption. These arrangements persist in showcasing the profound beauty and versatility of mathematics as a subject for exploration and innovation.