1.
Introduction
2.
Magic Square
3.
Properties of Magic Squares
4.
Algorithm for Doubly-even Magic Square :
5.
Implementation of Magic Square for doubly-even:
5.1.
C++
5.1.1.
Output
6.
6.1.
For what reason are magic squares called magic?
6.2.
How would you tackle a magic square question?
6.3.
What is the logic behind magic square?
7.
Conclusion
Last Updated: Mar 27, 2024

# Magic Square (Even order)

Neha kumari
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

## Introduction

In this article, we will discuss the even-order magic square and its implementation in different languages. Magic squares are one of the most basic types of logic puzzles, and they're a terrific way to get started with problem-solving approaches that aren't limited to typical arithmetic procedures. Each square is divided into cells, and the rules state that every row, column, or diagonal in the square must add up to the same number.

Recommended Topic, Array Implementation of Queue and  Rabin Karp Algorithm

## Magic Square

A magic square of sequence n is the positioning of n2  numbers, generally substantial numbers, in a square, with the end goal that the n numbers in all rows, all columns, and the two diagonals total to a similar value. A magic square holds the integers from 1 to n2.

In the magic square, the constant value equals the sum of each row, column, and matrix diagonals.This constant value is calculated with: n(n2+1)/2.Where n is the constant value here. This magic constant of any square relies on the value of n.

For example Magic square of order 3

source:future learn

Magic square of order 4:

source: myworldtheirway

Based on the order of squares, the magic square consists of the following categories:

1) Magic Square of Odd Order. Example: 1,3,7,… (2*n +1)

2) Magic Square of Doubly-even order. Example : 6,12,18,21,.. (4*n)

3) Magic Square of Singly-even order. Example : 4,8,12,16,..(4*n +2)

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Properties of Magic Squares

1. 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(n2+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.
2. 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.
3. The transposition of a magic square is likewise a magic square.
4. 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.
5. 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 1-2-3-4 exchange.
An absolute best (MP) magic square MM is of doubly-even request (n = 4, 8, . . .) and has the following properties:
1. Two components that are n/2 components separated along all diagonals (counting broken ones in the two directions) aggregate to n2 +1.
2. So, The members of each one of the 2 by 2 subsquares (depending on broken top-base, broken left-right, and the four corners) aggregate to the consistent 2n2 +1.

## Algorithm for Doubly-even Magic Square :

Define a 2-D array of order n*n: The two-dimensional 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 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 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 doubly-even:

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.

### 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 up-one, right-one 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 n2 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 doubly-even 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.

Live masterclass