Magic Square matrix in C

What is a magic square?

A magic square is a square grid filled with integers where the total of the numbers in each row, column, and diagonal is the same. This constant sum is known as the "magic constant" or "magic sum." Magic squares are arranged in different patterns to denote the number of rows and columns in the grid. The most frequent magic squares are odd-order magic squares (e.g., 3x3, 5x5, 7x7) and even-order magic squares (e.g., 4x4, 8x8).

History

1. Origins Prior to 200 BCE:

  • The Lo Shu Square, the first known magical square, goes back to roughly 2200 BCE in ancient China. The Lo Shu Square is said to have formed on the back of a turtle coming from the Lo River. It has a magic constant of 15 and is a 3x3 magic square. The Lo Shu Square's importance was firmly steeped in Chinese mysticism.

2. Chinese Invasion (200 BCE - 600 CE):

  • During this time, magic squares rose to prominence in Chinese mathematics. Yang Hui (1238-1298), a well-known Chinese mathematician, made substantial contributions to the study of magic squares.

3. Middle Eastern Developments (from the eighth to the thirteenth centuries):

  • Magic squares were known across the Islamic world. Magic squares were examined and written about by scholars such as Al-Kindi and Al-Buni, who emphasized their mathematical and artistic merits.
  • Magic squares were mentioned in Al-Khwarizmi's book "Kitab al-Jabr wal-Muqabala" (The Compendious Book on Calculation by Completion and Balancing).

4. European Renaissance (15th-16th centuries):

  • During the Renaissance, European mathematicians and painters were intrigued by magic squares.
  • In his etching "Melencolia I" (1514), the great German artist and mathematician Albrecht Dürer produced a famous 4x4 magic square. This square is both mathematically and visually interesting.

5. Revolutions in India (17th century):

  • During the 17th century, Indian mathematicians such as Narayana Pandit made significant contributions to the magic square theory.

6. Enlightenment in Europe (17th-18th centuries):

  • European mathematicians such as Leonard Euler and Leonhard Christoph Sturm investigated the characteristics of magic squares, resulting in significant discoveries and contributions.

7. Modern Mathematics (from the nineteenth century to the present):

  • The study of magic squares evolved alongside the development of contemporary mathematics. Significant contributions were made by mathematicians like Édouard Lucas, Gaston Tarry, and John Conway.
  • A key emphasis of research became the categorization of all conceivable orders of magic squares, as well as their attributes.

8. Recreational Mathematics and Art (from the twentieth century to the present):

  • Magic squares have been used in a variety of industries, including recreational mathematics and painting. Magic square puzzles and games are still popular.
  • For some mathematicians, creating enormous, intricate magic squares has become a competitive endeavor.

Properties and characteristics

Constant sum: The total of the integers in each row, column, and diagonal of a magic square is always the same, which is known as the "magic constant" or "magic sum." Magic squares are distinguished from conventional square grids of integers by this characteristic.

Order: The number of rows and columns in a magic square's grid is referred to as its order. Magic square orders that are commonly used include 3x3, 4x4, 5x5, 7x7, and so on. Even-order magic squares (e.g., 4x4, 8x8) were discovered lately and exhibit unique qualities, but odd-order magic squares (e.g., 3x3, 5x5) have been known for millennia.

Normal Magic Square: A "normal" magic square comprises consecutive positive numbers beginning with 1. Normal magic squares are of great importance in mathematical study because they are the most basic and elegant type of magic square.

Calculating the Magic Constant: The magic constant for a given magic square of order n is (n * (n2 + 1)) / 2. This formula guarantees the total of each row, column, and diagonal.

Example: 3 x 3 matrix

In this 3x3 magic square:

The order is 3x3, meaning it has 3 rows and 3 columns.

The magic constant (the sum of each row, column, and diagonal) is 15.

We can verify that this is a magic square by adding the numbers in each row, column, and diagonal:

  • Row 1: 2 + 7 + 6 = 15
  • Row 2: 9 + 5 + 1 = 15
  • Row 3: 4 + 3 + 8 = 15
  • Column 1: 2 + 9 + 4 = 15
  • Column 2: 7 + 5 + 3 = 15
  • Column 3: 6 + 1 + 8 = 15
  • Diagonal from top-left to bottom-right: 2 + 5 + 8 = 15
  • Diagonal from top-right to bottom-left: 6 + 5 + 4 = 15

Some well-known magic squares

1. Lo Shu Square (3x3)

The roots of the Lo Shu Square may be traced back over two millennia in Chinese tradition. This 3x3 magical square appeared on the back of a supernatural turtle emerging from the Lo River, according to ancient tradition. Its earliest appearance is attributed to the mythical Emperor Yu (around 2200 BCE), who is credited with discovering the square's layout.

Numerical Arrangement: The Lo Shu Square has the numbers 1 through 9, which are placed in a precise way within the 3x3 grid. The numbers are organized so that the total of each row, column, and diagonal always equals 15.

Example

In the Lo Shu Square:

  • The order is 3x3, meaning it has three rows and three columns.
  • The magic constant (the sum of each row, column, and diagonal) is 15.

2. Albrecht Dürer's magic square

Albrecht Dürer's magic square is a well-known 4x4 magic square designed in 1514 by German artist and mathematician Albrecht Dürer. Dürer's magical square is notable not just for its mathematical qualities but also for its artistic inventiveness. Here's a rundown of Dürer's magical square:

Magic Square by Dürer (4x4):

  • The order is 4x4, which means there are four rows and four columns.
  • Dürer's square comprises the numbers 1 through 16, which are arranged in a 4x4 grid. The unique manner in which the numerals are put is what distinguishes this square.
  • Dürer's square has a magical constant of 34, which means that the total of each row, column, and diagonal equals 34.

Example

It is the well-known Dürer's 4x4 magic square. In this square:

  • The arrangement is 4x4, which means it has four rows and four columns.
  • The magic constant (the total of each row, column, and diagonal) is 34.

3. Sagrada Família magic square

The Sagrada Famlia magic square is a relatively new and one-of-a-kind example of a magic square, well recognised for its relationship with Barcelona's iconic Sagrada Famlia basilica. Antoni Gaud, the basilica's architect, included a magical square in the architecture of the basilica's façade.

The Sagrada Famlia magic square is a 4x4 square that is related to the basilica's completion date. The sum of the square's rows, columns, and diagonals is 33. This number is significant in Christianity because it is thought to represent the age of Jesus Christ at the time of his crucifixion and resurrection.

In this enchanted square

  • Because the total of each row, column, and diagonal equals 33, the magic constant is 33.
  • The incorporation of the plaza in the façade of the Sagrada Famlia serves as a symbolic and significant element, tying the basilica's construction to the Christian religion.
  • Antoni Gaud was a very spiritual man who infused religious and mystical symbolism into his architectural creations.
  • The usage of a magical square in the Sagrada Famlia is a one-of-a-kind example of how mathematics and symbolism may be used in art and architecture.

4. Franklin's Magic Square

The 8x8 magic square by Benjamin Franklin is a spectacular example of a big magic square recognized for its intricacy and unique mathematical features. Benjamin Franklin, one of the United States Founding Fathers, devised this square, which consists of 64 positive and negative integers.

  • The order is 8x8, which implies there are eight rows and eight columns.
  • Franklin's 8x8 magic square comprises the numbers 1 to 64, which are arranged in a complex design. It covers both positive and negative numbers, unlike regular magic squares.
  • Franklin's square has a magic constant of 260, which means that the total of each row, column, and diagonal equals 260.

Example

In Benjamin Franklin's 8x8 magic square:

  • The arrangement is 8x8, which means there are eight rows and eight columns.
  • The magical constant is 260 (the total of each row, column, and diagonal).

Approach 1:Magic Squares in Odd Order

Odd-order magic squares are a magic square that has an odd number of rows and columns. The sum of the integers in each row, column, and diagonal of an odd-order magic square is the same, which is known as the "magic constant." These squares are frequently depicted as N × N grids, where N is an odd integer (for example, 3x3, 5x5, 7x7, and so on).

Example

Consider a 3x3 odd-order magic square as an example. The magic constant in a 3x3 magic square is (322 + 1) / 2 = 10/2 = 5.

An example of a 3x3 odd-order magic square is shown below:

In this magic square, you can observe that the sum of each row, each column, and both diagonals is equal to the magic constant, which is 5.

For instance:

  • The sum of the first row (2 + 7 + 6) is 15 (the magic constant).
  • The sum of the second row (9 + 5 + 1) is also 15.
  • The sum of the third row (4 + 3 + 8) is 15.
  • The sum of the first column (2 + 9 + 4) is 15.
  • The sum of the second column (7 + 5 + 3) is 15.
  • The sum of the third column (6 + 1 + 8) is 15.
  • The sum of the main diagonal (2 + 5 + 8) is 15.
  • The sum of the other diagonal (6 + 5 + 4) is 15.

Implementation

Output:

Magic Square matrix in C

This example demonstrates the basic principles of an odd-order magic square, in which the magic constant assures that the total of all rows, columns, and diagonals is the same.

Approach 2: Higher-Dimensional Magic Squares

Higher-dimensional magic squares are those that occur in dimensions other than the standard 2D square grids. Magic squares' idea may be expanded to higher dimensions, including magic cubes and magic hypercubes. These three-dimensional constructions are also referred to as "magic hypercubes."

Example

Creating a 3x3x3 magic hypercube is a difficult mathematical undertaking, and visualizing it in language may be difficult. However, as a highly abstract and simplified example, I can propose a basic depiction of a 3x3x3 magic hypercube. In this depiction, we'll utilize integers ranging from 1 to 27 in a 3x3x3 grid.

42 is the magic constant for a 3x3x3 magic hypercube.

Here's a simple illustration of a 3x3x3 magic hypercube:

Layer 1:

[ 2 7 6]

[ 9 5 1]

[ 4 3 8]

Layer 2:

[11 16 15]

[18 14 10]

[13 12 17]

Layer 3:

[20 25 24]

[27 23 19]

[22 21 26]

This very simplified picture depicts what a 3x3x3 magic hypercube would look like. Building a 3x3x3 magic hypercube that genuinely satisfies the features of a magical hypercube, where the sum of integers along multiple hyperplanes equals the magic constant, is a considerably more difficult mathematical procedure.

Implementation

Output:

Magic Square matrix in C

Approach 3: Symmetry

A basic and visually pleasing aspect of odd-order magic squares is symmetry. Reflectional (rotational) and diagonal symmetry are two forms of symmetry found in odd-order magic squares. These symmetries add to the squares' visual and mathematical beauty. Let's take a closer look at these sorts of symmetry:

Example

2 7 6

9 5 1

4 3 8

A 3x3 Odd-Order Magic Square with Symmetry:

1. Symmetry of Reflection (Horizontal and Vertical):

  • Folding the square along its horizontal axis (i.e., the middle row) reveals that the top and bottom parts are mirror copies of each other. The upper half's numbers are mirrored in the lower half.
  • Folding the square along its vertical axis (i.e., the center column) reveals that the left and right half are mirror copies of each other. The numerals on the left side are mirrored on the right.

2. Symmetrical Rotation:

  • 90-Degree Rotation: If you rotate the square 90 degrees clockwise, its arrangement is preserved. In other words, even if the numbers are rotated 90 degrees, they still form a valid 3x3 odd-order magic square.

3. Symmetry of Diagonal Lines:

  • The square has diagonal symmetry along its primary diagonal, which goes from the top-left to the bottom-right corner. The numerals on one side of the diagonal are mirror images of the ones on the other.

Implementation

Output:

Magic Square matrix in C

Applications

  1. Coding and Cryptography: Magic squares are used in coding theory and cryptography. They may be used to encrypt and decode messages, adding an extra degree of protection.
  2. Sudoku: Sudoku puzzles, which have grown in popularity across the world, are simply 9x9 magic squares. Solving sudoku problems entails arranging numbers so that they fulfill precise limitations, similar to building a magic square.
  3. Game Design: Magic squares have appeared in a variety of board games and puzzles. They provide a distinct and difficult aspect to the gameplay.
  4. Recreational Mathematics: Magic squares captivate recreational mathematicians and puzzle aficionados. The task of solving and constructing magic squares is both enjoyable and intellectually engaging.
  5. Art and Design: Magic squares have been utilized to produce visually beautiful patterns in art and design. Magic squares were used by artists such as Albrecht Dürer to enhance the visual attractiveness of their work.