Javatpoint Logo
Javatpoint Logo

Magic Square Function in C

In this article, we will discuss the magic square function in C with its examples.

A magic square of order n is an arrangement of n2 numbers, often different integers, in a square such that the total of the n numbers in the square's diagonals, rows, and columns is a fixed value. Integers from 1 to n2 are contained within a magic square.

The magic constant, or magic sum, M, is the total that is constant in each row, column, and diagonal. A typical magic square's magic constant, which only depends on n, has the following value:

M = n(n2+1)/2

Let's consider the normal magic squares of order n = 6, 7, 8, ...

For these orders, the magic constants are approximately: 372, 509, 660, 825, ...

The first integer, 1, is kept at position (n/2, n-1) in any magic square. Let the coordinates be (i,j). The following number is kept at position (i-1, j+1), where each row and column can be thought of as a circular array because they wrap around.

Three requirements are accomplished:

The position of the subsequent number is determined by decrementing the previous number's row number by 1, and increasing the previous number's column number by 1. The estimated row position will always wrap around to n-1 if it ever gets negative. The calculated column position will also wrap around to zero if it rises to n.

Example:

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 take a program to understand the use of the magic square function in C.

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 take another program to understand the use of the magic square function in C.

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 conclusion, magic squares are unique configurations of numbers that have the special characteristic of having equal sums along all of the rows, columns, and diagonals. Throughout history, they have enthralled mathematicians, artists, and societies. Magic squares offer a rich junction of mathematics and other fields, from their mathematical complexity and aesthetic appeal in art to their cultural meaning and practical uses in encryption. These configurations continue to demonstrate mathematics' great beauty and adaptability as a topic for investigation and creation.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA