Magic Square | GeeksforGeeks
A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2.
A magic square of order n is an arrangement of n^2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A magic square contains the integers from 1 to n^2.
The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the following value:
M = n(n^2+1)/2
M = n(n^2+1)/2
For normal magic squares of order n = 3, 4, 5, …, the magic constants are: 15, 34, 65, 111, 175, 260, …
1. The position of next number is calculated by decrementing row number of previous number by 1, and incrementing the column number of previous number by 1. At any time, if the calculated row position becomes -1, it will wrap around to n-1. Similarly, if the calculated column position becomes n, it will wrap around to 0. (-1,+1)
2. If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1. (+1,-2)
3. If the calculated row position is -1 & calculated column position is n, the new position would be: (0, n-2). (-1,n) -> (0, n-2)
void
generateSquare(
int
n)
{
int
magicSquare[n][n];
// set all slots as 0
memset
(magicSquare, 0,
sizeof
(magicSquare));
// Initialize position for 1
int
i = n/2;
int
j = n-1;
// One by one put all values in magic square
for
(
int
num=1; num <= n*n; )
{
if
(i==-1 && j==n)
//3rd condition
{
j = n-2;
i = 0;
}
else
{
//1st condition helper if next number goes to out of square's right side
if
(j == n)
j = 0;
//1st condition helper if next number is goes to out of square's upper side
if
(i < 0)
i=n-1;
}
if
(magicSquare[i][j])
//2nd condition
{
j -= 2;
i++;
continue
;
}
else
magicSquare[i][j] = num++;
//set number
j++; i--;
//1st condition
}
// print magic square
printf
(
"The Magic Square for n=%d:\nSum of each row or column %d:\n\n"
,
n, n*(n*n+1)/2);
for
(i=0; i<n; i++)
{
for
(j=0; j<n; j++)
printf
(
"%3d "
, magicSquare[i][j]);
printf
(
"\n"
);
}
}
public static void main(String[] args) { int N = Integer.parseInt(args[0]); if (N % 2 == 0) throw new RuntimeException("N must be odd"); int[][] magic = new int[N][N]; int row = N-1; int col = N/2; magic[row][col] = 1; for (int i = 2; i <= N*N; i++) { if (magic[(row + 1) % N][(col + 1) % N] == 0) { row = (row + 1) % N; col = (col + 1) % N; } else { row = (row - 1 + N) % N; // don't change col } magic[row][col] = i; } // print results for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (magic[i][j] < 10) System.out.print(" "); // for alignment if (magic[i][j] < 100) System.out.print(" "); // for alignment System.out.print(magic[i][j] + " "); } System.out.println(); } }http://comproguide.blogspot.com/2013/07/program-to-generate-magic-square.html
public static void main(String[] args) { Scanner reader = new Scanner(System.in); int n = reader.nextInt(); int[][] board = new int[n][n]; //automatically initialized with '0' int row = 0; // start with first row int col = n/2; //middle element int num = 1; // number to be filled // n X n grid contains n^2 numbers while ( num <= n*n ) { board[row][col] = num; num++; //increment column, when reaches last, start from the beginning int tempCol = (col + 1)%n; //decrement row, when last row is reached, move to the first int tempRow = (row - 1) >= 0 ? row-1 : n-1; //If the target cell is not empty, move to next row if( board[tempRow][tempCol] != 0 ) { row = (row+1)%n; } else { row = tempRow; col = tempCol; } } //print the magix square for( int i = 0 ; i < n ; i++) { for( int j = 0 ; j < n ; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } }http://rosettacode.org/wiki/Magic_squares_of_odd_order#Java
Video:
https://www.youtube.com/watch?v=jod39YEGMCU
https://www.youtube.com/watch?v=c-htnWkiM4Q
Read full article from Magic Square | GeeksforGeeks