Javatpoint Logo
Javatpoint Logo

Magic Square in Java

A magic square is a square consisting of numbers. A magic square of the order n has the numbers from 1 to m2 (1 and m2 inclusive) in such a way that the sum of all the numbers present in a row is equal to the sum of all the numbers present in a column, which in turn is equal to the sum of all the numbers present in a diagonal. If we represent the sum as M, then M is completely dependent on m. Mathematically M has the following value.

M = m(m2 + 1) / 2.

Thus,

For m = 3,

M = 3 x (32+ 1) / 2 = (3 x (9 + 1)) / 2 = (3 x 10) / 2 = 15

For m = 5,

M = 5 x (52+ 1) / 2 = (5 x (25 + 1)) / 2 = (5 x 26) / 2 = 130 / 2 = 65

For m= 7,

M = 7 x (72+ 1) / 2 = (7 x (49 + 1)) / 2 = (7 x 50) / 2 = 175

For m= 9,

M = 9 x (92+ 1) / 2 = (9 x (81 + 1)) / 2 = (9 x 82) / 2 = 369

Let's see a few examples of magic squares.

For m = 9, we have a magic square of 9 x 9, where each row, column, and diagonal have a value of 369.

For m = 11, we have a magic square of 11 x 11, where each row, column, and diagonal have the value of 671.

Rules for Filling the Magic Square

Let's discuss the ideas which will help us in writing the rules for filling the magic square. One thing we know for sure is that every number from 1 to m2 comes in the magic square. We will put the number 1 at the location (m / 2, m - 1), and then from there, we will consider each row and column as circular and hence will wrap around.

Rule 1: At any position, the position of the next number is computed by decreasing the row number of the previously filled number by 1 and increasing the column number of the previously filled number by 1. At any point of time, if the calculated position of the row becomes -1, then we will wrap it around to m - 1. Similarly, if the calculated position of the column becomes m, then we will wrap it around to 0.

Rule 2: If at the computed position, a number is already present in the magic square, then the computed position of the column is decreased by 2, and the computed position of the row is increased by 1.

Rule 3: If the computed position of the row is -1 & the computed position of the column is m, then the new position will be: (0, m - 2).

Let's take an example for a better understanding.

276

951

438

The above square matrix is of order 3, containing 9 elements.

The position of the number 1 is (3 / 2, 3 - 1) = (1, 2)

The position of the number 2 is (1 - 1, 2 + 1) = (0, 3) = (0, 0) [3 is wrapped around to 0]

The position of the number 3 is (0 - 1, 0 + 1) = (-1, 1) = (3 - 1, 1) = (2, 1)

[-1 is wrapped around to 3 - 1 = 2]

The position of the number 4 is (2 - 1, 1 + 1) = (1, 2). However, the position of (1, 2) is already occupied by the number 1. Therefore, rule number 2 is applied, and we get (1 + 1, 2 - 2) = (2, 0).

The position of the number 5 is (2 - 1, 0 + 1) = (1, 1)

The position of the number 6 is (1 - 1, 1 + 1) = (0, 2)

The position of the number 7 is ( 0 - 1, 2 + 1) = (-1, 3) = (0, 3 - 2) = (0, 1)

[Rule number 3 is applied here]

The position of the number 8 is ( 0 - 1, 1 + 1) = (-1, 2) = (3 - 1, 2) = (2, 2)

[-1 is wrapped around to 3 - 1 = 2]

The position of the number 9 is ( 2 - 1, 2 + 1) = (1, 3) = (1, 0)

[3 is wrapped around to 0]

Implementation

Based on the above example, the following code is written.

FileName: MagicSquare.java

Output:

The Magic Square for 11: 

Sum of each column or row 671: 

54 42 30 18 6 115 103 91 79 67 66 
41 29 17 5 114 102 90 78 77 65 53 
28 16 4 113 101 89 88 76 64 52 40 
15 3 112 100 99 87 75 63 51 39 27 
2 111 110 98 86 74 62 50 38 26 14 
121 109 97 85 73 61 49 37 25 13 1 
108 96 84 72 60 48 36 24 12 11 120 
95 83 71 59 47 35 23 22 10 119 107 
82 70 58 46 34 33 21 9 118 106 94 
69 57 45 44 32 20 8 117 105 93 81 
56 55 43 31 19 7 116 104 92 80 68

Time Complexity: The time complexity of the above program is O(n2), where n is the total number of rows or columns present in the square matrix.

Space Complexity: The space complexity of the above program is O(n2), where n is the total number of rows or columns present in the square matrix.







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