Magic Square of Odd Order
A magic square is a square matrix in which the sum of the elements in each row, column, and both main diagonals is the same. In this post, we will explore the generation of magic squares of odd order using a specific algorithm. The algorithm involves placing numbers in a specific manner to achieve the magic square property.
![Example magic square of odd order [7X7] Example magic square of odd order [7X7]](./px/matrix/example-magic-square-of-odd-order-7x7.png)
Each row, column, and diagonal adds up to 175:
- Rows: 175, 175, 175, 175, 175, 175, 175
- Columns: 175, 175, 175, 175, 175, 175, 175
- Diagonals: 175, 175
Problem Statement
Given an odd integer n
, the goal is to create a magic square of size n x n
where
n
is odd. The task is to fill this square with numbers in such a way that the sum of each row, each
column, and both main diagonals is the same.
Explanation with an Example
Let's take the example of a 3x3 magic square to understand the concept.
2 7 6
9 5 1
4 3 8
In this example, each row, column, and diagonal adds up to 15. This property is what defines a magic square.
Idea to Solve the Problem
The algorithm to generate a magic square involves a methodical approach to filling in the numbers. The key idea is to start from the middle of the first row and then move diagonally up and to the right. If a filled cell is encountered, the position is adjusted accordingly. The numbers are filled in a cyclic manner while moving in the specified diagonal direction.
Pseudocode
magicSquare(n)
if n <= 1 or n is even
return
create a matrix of size n x n, initialized with zeros
set initial row = 0
set initial col = n - 1
set value = 1
while value <= n * n
if row is -1 and col is n
col = n - 2
row = 0
if col is n
col = 0
if row is less than 0
row = n - 1
if row is n
row = 0
if matrix[row][col] is 0
assign value to matrix[row][col]
decrement row by 1
increment col by 1
increment value by 1
else
increment row by 1
decrement col by 2
Algorithm Explanation
- Initialize the matrix with zeros to represent empty cells.
- Start from the middle of the first row (
row = 0
,col = n - 1
). - Loop through each value from
1
ton * n
. - Check the validity of the current position
(row, col)
and handle edge cases where the position goes out of bounds. - If the current cell is empty (
matrix[row][col] == 0
), fill it with the current value and move diagonally up and to the right. - If the current cell is already filled, adjust the position to move down and to the left.
Code Solution
// C Program
// Magic Square of Odd length
#include <stdio.h>
// Find magic square of Odd length
void magicSquare(int n)
{
if (n <= 1 || n % 2 == 0)
{
// This are not work when number is Even and less than 3
return;
}
int matrix[n][n];
int row = 0;
int col = 0;
int value = 1;
// Set default value
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
matrix[row][col] = 0;
}
}
// Get first location
row = n / 2;
col = n - 1;
// Execute loop through by n*n
while (value <= n *n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
// Change element value
value++;
}
else
{
row = row + 1;
col = col - 2;
}
}
printf(" Magic square of size (%dX%d) \n", n, n);
// Display result
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
printf(" %d", matrix[row][col]);
}
printf("\n");
}
printf(" Sum of each rows and columns is %d \n\n", (n *(n *n + 1) / 2));
}
int main(int argc, char
const *argv[])
{
magicSquare(7);
magicSquare(9);
magicSquare(3);
return 0;
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
/*
Java Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
public void magicSquare(int n)
{
if (n <= 1 || n % 2 == 0)
{
// This are not work when number is Even and less than 3
return;
}
int[][] matrix = new int[n][n];
int row = 0;
int col = 0;
int value = 1;
// Set default value
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
matrix[row][col] = 0;
}
}
// Get first location
row = n / 2;
col = n - 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
// Change element value
value++;
}
else
{
row = row + 1;
col = col - 2;
}
}
System.out.print(" Magic square of size (" + n + "X" + n + ") \n");
// Display result
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
System.out.print(" " + matrix[row][col]);
}
System.out.print("\n");
}
System.out.print(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
}
public static void main(String[] args)
{
Square task = new Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Magic Square of Odd length
*/
class Square
{
public:
// Find magic square of Odd length
void magicSquare(int n)
{
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
int matrix[n][n];
int row = 0;
int col = 0;
int value = 1;
// Set default value
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
matrix[row][col] = 0;
}
}
// Get first location
row = n / 2;
col = n - 1;
// Execute loop through by n*n
while (value <= n *n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Change element value
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
value++;
}
else
{
row = row + 1;
col = col - 2;
}
}
cout << " Magic square of size (" << n << "X" << n << ") \n";
// Display result
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
cout << " " << matrix[row][col];
}
cout << "\n";
}
cout << " Sum of each rows and columns is " << (n *(n *n + 1) / 2) << " \n\n";
}
};
int main()
{
Square task = Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
return 0;
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
// Include namespace system
using System;
/*
C# Program for
Magic Square of Odd length
*/
public class Square
{
// Find magic square of Odd length
public void magicSquare(int n)
{
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
int[,] matrix = new int[n,n];
int row = 0;
int col = 0;
int value = 1;
// Set default value
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
matrix[row,col] = 0;
}
}
// Get first location
row = n / 2;
col = n - 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row,col] == 0)
{
// Change element value
// Put value
matrix[row,col] = value;
row = row - 1;
col = col + 1;
value++;
}
else
{
row = row + 1;
col = col - 2;
}
}
Console.Write(" Magic square of size (" + n + "X" + n + ") \n");
// Display result
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
Console.Write(" " + matrix[row,col]);
}
Console.Write("\n");
}
Console.Write(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
}
public static void Main(String[] args)
{
Square task = new Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
<?php
/*
Php Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
public function magicSquare($n)
{
// This are not work when number is Even and less than 3
if ($n <= 1 || $n % 2 == 0)
{
return;
}
$matrix = array_fill(0, $n, array_fill(0, $n, 0));
// Get first location
$row = intval($n / 2);
$col = $n - 1;
$value = 1;
// Execute loop through by n*n
while ($value <= $n * $n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if ($row == -1 && $col == $n)
{
// When row is -1 and column is equal to given n
$col = $n - 2;
$row = 0;
}
if ($col == $n)
{
// When columns are out of range
$col = 0;
}
if ($row < 0)
{
// When row value less than zero
$row = $n - 1;
}
if ($row == $n)
{
// When row are out of range
$row = 0;
}
if ($matrix[$row][$col] == 0)
{
// Change element value
// Put value
$matrix[$row][$col] = $value;
$row = $row - 1;
$col = $col + 1;
$value++;
}
else
{
$row = $row + 1;
$col = $col - 2;
}
}
echo " Magic square of size (". $n ."X". $n .") \n";
// Display result
for ($row = 0; $row < $n; ++$row)
{
for ($col = 0; $col < $n; ++$col)
{
echo " ". $matrix[$row][$col];
}
echo "\n";
}
echo " Sum of each rows and columns is ". (intval($n * ($n * $n + 1) / 2)) ." \n\n";
}
}
function main()
{
$task = new Square();
$task->magicSquare(7);
$task->magicSquare(9);
$task->magicSquare(3);
}
main();
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
/*
Node Js Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
magicSquare(n)
{
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
var matrix = Array(n).fill(0).map(() => new Array(n).fill(0));
// Get first location
var row = parseInt(n / 2);
var col = n - 1;
var value = 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Change element value
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
value++;
}
else
{
row = row + 1;
col = col - 2;
}
}
process.stdout.write(" Magic square of size (" + n + "X" + n + ") \n");
// Display result
for (row = 0; row < n; ++row)
{
for (col = 0; col < n; ++col)
{
process.stdout.write(" " + matrix[row][col]);
}
process.stdout.write("\n");
}
process.stdout.write(" Sum of each rows and columns is " + (parseInt(n * (n * n + 1) / 2)) + " \n\n");
}
}
function main()
{
var task = new Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
main();
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
# Python 3 Program for
# Magic Square of Odd length
class Square :
# Find magic square of Odd length
def magicSquare(self, n) :
# This are not work when number is Even and less than 3
if (n <= 1 or n % 2 == 0) :
return
matrix = [[0] * (n) for _ in range(n) ]
# Get first location
row = int(n / 2)
col = n - 1
value = 1
# Execute loop through by n*n
while (value <= n * n) :
# row and column indicate element position but
# Before accessing the elements we check its valid limit
if (row == -1 and col == n) :
# When row is -1 and column is equal to given n
col = n - 2
row = 0
if (col == n) :
# When columns are out of range
col = 0
if (row < 0) :
# When row value less than zero
row = n - 1
if (row == n) :
# When row are out of range
row = 0
if (matrix[row][col] == 0) :
# Change element value
# Put value
matrix[row][col] = value
row = row - 1
col = col + 1
value += 1
else :
row = row + 1
col = col - 2
print(" Magic square of size (", n ,"X", n ,") ")
# Display result
row = 0
while (row < n) :
col = 0
while (col < n) :
print(" ", matrix[row][col], end = "")
col += 1
print(end = "\n")
row += 1
print(" Sum of each rows and columns is ", (int(n * (n * n + 1) / 2)) ," \n")
def main() :
task = Square()
task.magicSquare(7)
task.magicSquare(9)
task.magicSquare(3)
if __name__ == "__main__": main()
Output
Magic square of size ( 7 X 7 )
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size ( 9 X 9 )
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size ( 3 X 3 )
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
# Ruby Program for
# Magic Square of Odd length
class Square
# Find magic square of Odd length
def magicSquare(n)
# This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
return
end
matrix = Array.new(n) {Array.new(n) {0}}
# Get first location
row = n / 2
col = n - 1
value = 1
# Execute loop through by n*n
while (value <= n * n)
# row and column indicate element position but
# Before accessing the elements we check its valid limit
if (row == -1 && col == n)
# When row is -1 and column is equal to given n
col = n - 2
row = 0
end
if (col == n)
# When columns are out of range
col = 0
end
if (row < 0)
# When row value less than zero
row = n - 1
end
if (row == n)
# When row are out of range
row = 0
end
if (matrix[row][col] == 0)
# Change element value
# Put value
matrix[row][col] = value
row = row - 1
col = col + 1
value += 1
else
row = row + 1
col = col - 2
end
end
print(" Magic square of size (", n ,"X", n ,") \n")
# Display result
row = 0
while (row < n)
col = 0
while (col < n)
print(" ", matrix[row][col])
col += 1
end
print("\n")
row += 1
end
print(" Sum of each rows and columns is ", (n * (n * n + 1) / 2) ," \n\n")
end
end
def main()
task = Square.new()
task.magicSquare(7)
task.magicSquare(9)
task.magicSquare(3)
end
main()
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
/*
Scala Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
def magicSquare(n: Int): Unit = {
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
var matrix: Array[Array[Int]] = Array.fill[Int](n, n)(0);
// Get first location
var row: Int = (n / 2).toInt;
var col: Int = n - 1;
var value: Int = 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix(row)(col) == 0)
{
// Change element value
// Put value
matrix(row)(col) = value;
row = row - 1;
col = col + 1;
value += 1;
}
else
{
row = row + 1;
col = col - 2;
}
}
print(" Magic square of size (" + n + "X" + n + ") \n");
// Display result
row = 0;
while (row < n)
{
col = 0;
while (col < n)
{
print(" " + matrix(row)(col));
col += 1;
}
print("\n");
row += 1;
}
print(" Sum of each rows and columns is " + ((n * (n * n + 1) / 2).toInt) + " \n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Square = new Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
/*
Swift 4 Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
func magicSquare(_ n: Int)
{
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
var matrix: [[Int]] = Array(repeating: Array(repeating: 0, count: n), count: n);
// Get first location
var row: Int = n / 2;
var col: Int = n - 1;
var value: Int = 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Change element value
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
value += 1;
}
else
{
row = row + 1;
col = col - 2;
}
}
print(" Magic square of size (", n ,"X", n ,") ");
// Display result
row = 0;
while (row < n)
{
col = 0;
while (col < n)
{
print(" ", matrix[row][col], terminator: "");
col += 1;
}
print(terminator: "\n");
row += 1;
}
print(" Sum of each rows and columns is ", (n * (n * n + 1) / 2) ," \n");
}
}
func main()
{
let task: Square = Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
main();
Output
Magic square of size ( 7 X 7 )
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size ( 9 X 9 )
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size ( 3 X 3 )
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
/*
Kotlin Program for
Magic Square of Odd length
*/
class Square
{
// Find magic square of Odd length
fun magicSquare(n: Int): Unit
{
// This are not work when number is Even and less than 3
if (n <= 1 || n % 2 == 0)
{
return;
}
var matrix: Array <Array<Int>> = Array(n){Array(n){0}};
// Get first location
var row: Int = n / 2;
var col: Int = n - 1;
var value: Int = 1;
// Execute loop through by n*n
while (value <= n * n)
{
// row and column indicate element position but
// Before accessing the elements we check its valid limit
if (row == -1 && col == n)
{
// When row is -1 and column is equal to given n
col = n - 2;
row = 0;
}
if (col == n)
{
// When columns are out of range
col = 0;
}
if (row < 0)
{
// When row value less than zero
row = n - 1;
}
if (row == n)
{
// When row are out of range
row = 0;
}
if (matrix[row][col] == 0)
{
// Change element value
// Put value
matrix[row][col] = value;
row = row - 1;
col = col + 1;
value += 1;
}
else
{
row = row + 1;
col = col - 2;
}
}
print(" Magic square of size (" + n + "X" + n + ") \n");
// Display result
row = 0;
while (row < n)
{
col = 0;
while (col < n)
{
print(" " + matrix[row][col]);
col += 1;
}
print("\n");
row += 1;
}
print(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: Square = Square();
task.magicSquare(7);
task.magicSquare(9);
task.magicSquare(3);
}
Output
Magic square of size (7X7)
20 12 4 45 37 29 28
11 3 44 36 35 27 19
2 43 42 34 26 18 10
49 41 33 25 17 9 1
40 32 24 16 8 7 48
31 23 15 14 6 47 39
22 21 13 5 46 38 30
Sum of each rows and columns is 175
Magic square of size (9X9)
35 25 15 5 76 66 56 46 45
24 14 4 75 65 55 54 44 34
13 3 74 64 63 53 43 33 23
2 73 72 62 52 42 32 22 12
81 71 61 51 41 31 21 11 1
70 60 50 40 30 20 10 9 80
59 49 39 29 19 18 8 79 69
48 38 28 27 17 7 78 68 58
37 36 26 16 6 77 67 57 47
Sum of each rows and columns is 369
Magic square of size (3X3)
2 7 6
9 5 1
4 3 8
Sum of each rows and columns is 15
Time Complexity
The time complexity of the algorithm is O(n^2) since each cell in the matrix is filled exactly once. The loop
iterates through n * n
values, and each iteration involves constant time operations.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment