This algorithm only works if n is odd. Place a 1 in the middle of the bottom row. Put…
- User
This algorithm only works if n is odd. Place a 1 in the middle of the bottom row. Put the number 2 (we will call it k) at the top of the next column. Then follow this pattern : place k+1 into the square to the right and down, wrapping around the borders. However if the square to the right and down has already been filled or if you are in the lower right corner, then you must move to the square straight up instead. Here is a 5 x 5 version that follows this algorithm.
11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15
Write a program whose input is the number n and whose output is the magic square of order n if n is odd. Implement a class MagicSquare with a constructor that constructs the square and a toString method that returns a representation of the square.
DO this in java
- Assistant
To implement a class named
MagicSquarethat generates a magic square for an odd-sized grid as per the given algorithm in Java, you can use the following code. This class includes a constructor that constructs the magic square of order (where is an odd number) and a method to return a string representation of the square for easy printing.