How to resolve the algorithm Magic squares of odd order step by step in the Java programming language
How to resolve the algorithm Magic squares of odd order step by step in the Java programming language
Table of Contents
Problem Statement
A magic square is an NxN square matrix whose numbers (usually integers) consist of consecutive numbers arranged so that the sum of each row and column, and both long (main) diagonals are equal to the same sum (which is called the magic number or magic constant). The numbers are usually (but not always) the first N2 positive integers. A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square.
For any odd N, generate a magic square with the integers 1 ──► N, and show the results here.
Optionally, show the magic number.
You should demonstrate the generator by showing at least a magic square for N = 5.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magic squares of odd order step by step in the Java programming language
The provided Java code defines a class MagicSquare
that generates a magic square of odd order. A magic square is an arrangement of numbers, usually integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant.
Implementation Details:
-
magicSquareOdd
Method:- This method takes an
int
parameterbase
which represents the order of the magic square (it has to be an odd number greater than 2). - It initializes a 2D array
grid
to represent the magic square, with a size ofbase
xbase
. - The method uses two variables
r
andc
to keep track of the current row and column, and a variablenumber
to keep track of the number being placed in the square. - It enters a
while
loop that runsbase * base
times to fill the grid with numbers. - Inside the loop:
- It places the current
number
in the grid at the currentr
andc
position. - It adjusts
r
andc
based on specific rules to determine the next position to place a number:- If it's on the first row, it either moves down or wraps around to the last row.
- If it's on the last column, it either moves left or wraps around to the first column.
- Otherwise, it moves diagonally down and right, or up if the space is already occupied.
- It places the current
- After filling the grid, it returns the
grid
as a 2D array.
- This method takes an
-
main
Method:- Creates a magic square of order 5 using the
magicSquareOdd
method and prints it to the console. - It also calculates and prints the magic constant, which is a constant sum for all rows, columns, and diagonals of a valid magic square.
- Creates a magic square of order 5 using the
Example Output:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Magic constant: 65
Source code in the java programming language
public class MagicSquare {
public static void main(String[] args) {
int n = 5;
for (int[] row : magicSquareOdd(n)) {
for (int x : row)
System.out.format("%2s ", x);
System.out.println();
}
System.out.printf("\nMagic constant: %d ", (n * n + 1) * n / 2);
}
public static int[][] magicSquareOdd(final int base) {
if (base % 2 == 0 || base < 3)
throw new IllegalArgumentException("base must be odd and > 2");
int[][] grid = new int[base][base];
int r = 0, number = 0;
int size = base * base;
int c = base / 2;
while (number++ < size) {
grid[r][c] = number;
if (r == 0) {
if (c == base - 1) {
r++;
} else {
r = base - 1;
c++;
}
} else {
if (c == base - 1) {
r--;
c = 0;
} else {
if (grid[r - 1][c + 1] == 0) {
r--;
c++;
} else {
r++;
}
}
}
}
return grid;
}
}
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Nim programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Scala programming language
You may also check:How to resolve the algorithm Mouse position step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the PARI/GP programming language