Properties of Magic Squares

The magic constant of a normal magic square relies on n, where n is the order of the magic square. The magic constant is given by the equation,
(M) = n(n^{2}+1)/2.
For other magic squares of request n = 3, 4, 5, 6, 7, 8 … the constants are 15, 34, 65, 111, 175, 265… individually.
 A magic square will stay magic on the off chance that any number is added to each digit of a magic square. A square will stay magic if any number increases each number of a magic square.
 The transposition of a magic square is likewise a magic square.
 A square will stay magic if two rows or columns, equidistant from the focus are exchanged. An even order square will stay magic if the diagonally opposite quadrants are exchanged.

An even request square will stay magic on the off chance that the sections or the lines are exchanged. If the first and the second row/columns are exchanged and the third and the fourth segments/columns are exchanged; this is known as a 1234 exchange.
An absolute best (MP) magic square MM is of doublyeven request (n = 4, 8, . . .) and has the following properties:
 Two components that are n/2 components separated along all diagonals (counting broken ones in the two directions) aggregate to n^{2} +1.
 So, The members of each one of the 2 by 2 subsquares (depending on broken topbase, broken leftright, and the four corners) aggregate to the consistent 2n^{2} +1.
Algorithm for Doublyeven Magic Square :
Define a 2D array of order n*n: The twodimensional array can be explained as an array of arrays. The 2D array is displayed as matrices which can be presented as a collection of rows and columns. However, 2D arrays are generated to implement a relational database lookalike data structure.
 Fill the array with their indexcounting 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 fixed 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];
}
Implementation of Magic Square for doublyeven:
Below is the implementation of Magic Square with the given algorithm in the C++ programming language.
C++
#include<iostream>
using namespace std;
void doublyEven( int n )
{
int arr[n][n], i, j;
for ( i = 0; i < n; i++)
for ( j = 0; j < n; j++)
arr[i][j] = (n*i) + j + 1;
for ( i = 0; i < n/4; i++)
for ( j = 0; j < n/4; j++)
arr[i][j] = (n*n + 1)  arr[i][j];
for ( i = 0; i < n/4; i++)
for ( j = 3 * (n/4); j < n; j++)
arr[i][j] = (n*n + 1)  arr[i][j];
for ( i = 3 * n/4; i < n; i++)
for ( j = 0; j < n/4; j++)
arr[i][j] = (n*n+1)  arr[i][j];
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];
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];
for (i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
cout << arr[i][j] << " ";
cout << "\n";
}
}
// driver program
int main()
{
int n=8;
doublyEven(n); //Function call
return 0;
}
Output
The same algorithm can also be implemented in other languages like Java, Python, PHP, and JavaScript similarly using their respective syntax.
Frequently Asked Question
For what reason are magic squares called magic?
Magic squares are called 'magic' because each line, column, and diagonal in the court has a similar total, called the magic steady. Total is the term we use for the answer to an additional issue.
How would you tackle a magic square question?
Fill in the leftover numbers utilizing an upone, rightone example. You will continuously fill in the numbers consecutively (1, 2, 3, 4, and so on) by moving up one row, then, at that point, one column to one side. You'll see that to put the number 2, you'll move over the top column of the magic square.
What is the logic behind magic square?
A magic square of request n is a game plan of n^{2} numbers, typically particular whole numbers, in a court, with the end goal that the n numbers in all rows, all columns, and the two diagonals aggregate to a similar consistency.
Conclusion
In this article, we have covered all about magic squares along with their three types. Along with the algorithms, basic code for doublyeven order magic square. We have examined the different orders of magic squares with constant n for each case, and their properties are also explained.
We hope this blog has helped you gain more knowledge about the given topic of Magic Square, and if you want to learn more, check out our articles at on Operations on Matrices. Upvote our blog to help other ninjas grow, and head to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.