Documentation
Input
Enter an odd number (n): 3
Output
Magic Square of size 3 × 3:
8 1 6
3 5 7
4 9 2
What is a Magic Square?
A magic square is an arrangement of numbers from 1 to n² such that the sum of numbers in each row, column, and both diagonals is the same.
Magic Constant:
M = n(n2+1) / 2
Algorithm (Siamese Method)
-
Start with number 1 in the middle of the top row
-
Next number goes up one row and right one column
-
If moving out of the square, wrap around
-
If the cell is already occupied, move down one row
-
Repeat until n² is placed