Experiment No.
– 6
Date of Performance: 29/01/25
Date of Submission: 5/02/25
Program Execution/
formation/ Timely
Viva Experiment
correction/ Submission Sign with Date
(03) Total (10)
ethical practices (01)
(06)
Aim: Implement program for generation of Magic square using
Nagarjuna algorithm
Lab Outcome: Implement algorithms developed by Ancient
Mathematicians
6.3 Related Theory:
A magic square is a special arrangement of distinct integers in a square grid such that the sum
of the numbers in every row, column, and both main diagonals equals the same value, known
as the magic constant. This concept has fascinated mathematicians for centuries and appears
in various cultures and historical artifacts.
The formula for finding the magic constant (M) in an 𝑛×𝑛 is:
M=n(n^2+1)/2
IKS-II Lab ITLER0IK202
1-1
Key Properties of Magic Squares:
1. Order:
The order of a magic square refers to the number of rows (or columns), denoted by \( n \).
Magic squares are classified as:
- Odd order (e.g., 3×3, 5×5)
- Even order (e.g., 4×4, 6×6)
- Doubly even order (multiples of 4, such as 4×4, 8×8)
2. Distinct Integers:
Typically, a magic square uses the consecutive integers from 1 to \( n^2 \). This constraint
adds to the challenge and elegance of constructing them.
3. Symmetry:
Magic squares often possess symmetrical patterns. These symmetries are useful in devising
methods for their construction, especially in odd and doubly even orders.
Magic squares hold significance not just in mathematics but also in art, mysticism, and
architecture, symbolizing harmony and balance.
IKS-II Lab ITLER0IK202
1-2
6.4 Algorithm:
Nagarjuna's Algorithm
The Nagarjuna algorithm is a systematic method for constructing odd-order magic squares. It
is named after the ancient Indian mathematician Nagarjuna. The algorithm is based on a
specific placement strategy that ensures the properties of a magic square are maintained.
Steps of the Nagarjuna Algorithm:
1. Initialization: Start with an empty ( n \times n ) grid filled with zeros.
2. Starting Position: Place the number 1 in the middle of the first row.
3. Placement Rule:
● For each subsequent number ( k ) (from 2 to ( n^2 )):
● Move up one row and right one column from the last placed number.
● If this position is outside the grid, wrap around to the opposite side
(using modulo operation).
● If the calculated position is already occupied, move directly down from
the last placed number instead.
4. Completion: Continue this process until all numbers from 1 to ( n^2 ) are placed in the
grid.
6.5 Program and Output:
#include <stdio.h>
void generateMagicSquare(int n) {
int magicSquare[n][n];
// Initialize the magic square with 0
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
magicSquare[i][j] = 0;
// Start position for 1
int row = 0;
IKS-II Lab ITLER0IK202
1-3
int col = n / 2;
// Fill the magic square using Nagarjuna's method
for (int num = 1; num <= n * n; num++) {
magicSquare[row][col] = num; // Place the current number
// Calculate the next position
int newRow = (row - 1 + n) % n; // Move up
int newCol = (col + 1) % n; // Move right
// If the calculated position is already filled, move down instead
if (magicSquare[newRow][newCol] != 0) {
row = (row + 1) % n; // Move down
} else {
row = newRow; // Move to the new position
col = newCol; // Move to the new position
}
}
// Print the magic square
printf("Magic Square of order %d:\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%3d ", magicSquare[i][j]);
}
printf("\n");
}
}
int main() {
int n;
printf("Enter the order of the magic square (odd number): ");
scanf("%d", &n);
// Check if the order is odd
if (n % 2 == 0) {
printf("Please enter an odd number.\n");
return 1;
}
generateMagicSquare(n);
return 0;
}
IKS-II Lab ITLER0IK202
1-4
Output:
IKS-II Lab ITLER0IK202
1-5
6.6 Conclusion:
In this experiment, we successfully implemented the Nagarjuna algorithm to generate
odd-order magic squares using C programming. Key takeaways include:
1. Magic Squares: We explored the properties of magic squares, where the sums of all
rows, columns, and diagonals are equal.
2. Nagarjuna Algorithm: This systematic method allowed us to construct magic squares
by placing numbers in a specific pattern, ensuring the required properties were
maintained.
3. Implementation: The C program effectively demonstrated the algorithm, handling
edge cases and utilizing modular arithmetic for grid navigation.
4. Verification: The generated magic squares were verified to confirm that they met the
criteria, with all sums equaling the magic constant.
5. Learning Outcomes: This experiment enhanced our understanding of algorithm design
and programming while providing practical experience in coding mathematical
algorithms.
Overall, the successful implementation of the Nagarjuna algorithm highlights the intersection
of mathematics and programming, paving the way for further exploration of more complex
mathematical constructs.
Bookmark message
Copy message
6.7 Questions:
1. Explain the work of Nagarjuna with magic square algorithm.
Nagarjuna was an ancient Indian mathematician and philosopher, often credited with
significant contributions to mathematics, particularly in the field of number theory. One of his
notable contributions is the Nagarjuna algorithm for constructing magic squares, specifically
odd-order magic squares.
Work of Nagarjuna with Magic Squares
Magic Square Definition: A magic square is a square grid filled with distinct integers
such that the sum of the numbers in each row, each column, and both main diagonals
is the same. This common sum is known as the magic constant.
IKS-II Lab ITLER0IK202
1-6
Nagarjuna's Algorithm: The algorithm for constructing odd-order magic squares
involves a systematic placement of numbers. The steps are as follows:
a. Initialization: Start with an empty ( n \times n ) grid.
b. Starting Position: Place the number 1 in the middle of the first row.
c. Placement Rule:
i. For each subsequent number, move up one row and right one column
from the last placed number.
ii. If this position is outside the grid, wrap around to the opposite side.
iii. If the calculated position is already occupied, move directly down from
the last placed number instead.
d. Completion: Continue this process until all numbers from 1 to ( n^2 ) are
placed in the grid.
Mathematical Significance: The magic square constructed using Nagarjuna's method
exhibits several interesting properties, including symmetry and the ability to generate
various patterns. The algorithm not only serves as a mathematical curiosity but also
has implications in combinatorial design and recreational mathematics.
2. Describe ancient units of measurements.
In the Vedic period, the system of measurement was deeply intertwined with daily
life, rituals, architecture, and astronomy. These units were derived from natural
observations and the human body, reflecting a practical and spiritual approach to
quantifying the world.
Linear Measurements
1. Angula (finger breadth):
- It was the base unit, equivalent to the breadth of a finger.
- Approximately 1.763 cm in modern terms.
- 12 angulas made a *vitasti*.
IKS-II Lab ITLER0IK202
1-7
2. Vitasti (span):
- The distance between the tip of the thumb and the little finger when the hand is
fully stretched.
- Around 21 cm.
- 2 vitastis made a *hasta*.
3. Hasta (cubit):
- From the elbow to the tip of the middle finger.
- About 45–50 cm.
- Used in constructing altars and sacred spaces.
4. Dhanusha or Danda (staff or bow):
- Roughly 96 angulas or about 1.8–2 meters.
- Commonly used in measuring land.
5. Yojana :
- A large unit of distance used in cosmology and geography.
- Its value varies in texts, typically between 8–13 km.
- Mentioned in epics like the Ramayana for describing long journeys.
Area and Volume
- Krosa :
- 1/4th of a Yojana, used to estimate distances between places.
- Around 2–3 km.
- Tulas and Adhaka :
- Used for measuring weight and volume, especially of grains or liquids.
Time-related Units
IKS-II Lab ITLER0IK202
1-8
- Time was also measured precisely using natural cycles:
- Nimisha : The blink of an eye (~16/75 seconds).
- Kala , Kshana , and Muhurta : Progressively larger units.
Usage and Legacy
These units were not rigid but flexible, often varying slightly based on context and
region. They were used in sacred architecture (like fire altars or *vedis*), land
measurement for agriculture, and celestial calculations. Ancient texts like the *Sulba
Sutras* describe precise geometric methods using these units, reflecting a high level
of mathematical understanding.
Overall, Vedic measurement systems were a blend of practicality and spirituality,
revealing an advanced culture attentive to precision and cosmic harmony.
IKS-II Lab ITLER0IK202
1-9