Different constructor methods can be divided into three categories: Odd-order Magic Square, and 4MLevel magic and 4M+ Level 2 magic, whereinMIs a natural number, level 2 magic does not exist.
1 odd-order magic Construction Method
(1) Place 1 in the middle column of the first row;
(2) The numbers starting from 2 until n × n are stored in sequence according to the following rules: Walking in the 45 ° direction, going up to the right, that is, the number of rows in each row is less than the number of rows in the previous one, and the number of columns is increased by 1.
(3) If the range of rows and columns exceeds the range of the matrix, it is bypassed. For example, if 1 is in row 1st, 2 should be placed in the bottom row, and the number of columns must also be added to 1;
(4) If there is an existing number at the position specified by the preceding rule, or the previous number is column N of row 1st, the next number is placed below the previous number.
/Begin {bmatrix} 8 & 1 & 6 // 3 & 5 & 7 // 4 & 9 & 2 // /End {bmatrix} "> |
2 An even-order magic square structure
Magic of 2.1 4 m level
(1) Fill in the numbers 1 to n × n in the matrix from top to bottom and from left to right.
(2) the number of two Diagonal Points in all the 4x4 sub-squares of the square matrix about the square matrix Center for symmetric exchange, that is, a (I, j) and a (n-1-i, n-1-j) switch. The number of all other locations remains unchanged.
/Begin {bmatrix}
1 & 2 & 3 & 4 //
5 & 6 & 7 & 8 //
9 & 10 & 11 & 12 //
13 & 14 & 15 & 16
/End {bmatrix} "> ------------->/begin {bmatrix}
1 & 15 & 14 & 4 //
12 & 6 & 7 & 9 //
8 & 10 & 11 & 5 //
13 & 3 & 2 & 16
/End {bmatrix} ">
Level 3 4 m + level 2 Magic Square
(1) first construct the magic square of the n-2 = 4 m, and then put it in the center of the n-order Magic Square
(2) Add 8 m + 2 to each number of 4 m magic squares;
(3) convert 1, 2 ,..., 8 m + 2 and .... (4 m + 2) 2 is arranged in the outer circle in pairs. For example, note that 8 m + 2 is not added to the 4m order of the center, and ensure that each logarithm is added = 1 + (4 m + 2) 2
/Begin {bmatrix}
1 & 9 & 34 & 33 & 32 & 2 //
6 & 11 & 25 & 24 & 14 & 31 //
10 & 22 & 16 & 17 & 19 & 27 //
30 & 18 & 20 & 21 & 15 & 7 //
29 & 23 & 13 & 12 & 26 & 8 //
35 & 28 & 3 & 4 & 5 & 36
/End {bmatrix} ">
4 sample code
4.1 creation of odd-order magic parties
/** <Br/> * @ Author: Contribute to Wikipedia according GNU <br/> * @ description: magic used to create odd data tables <br/> */<br/> public class magic_squre_odd {<br/> static int matrix [] []; <br/> static int N; <br/> Public static void magic_squre_odd_generate () <br/> {matrix = new int [N] [N]; <br/> // Initialize all data to 0 <br/> for (INT I = 0; I <n; I ++) <br/> for (Int J = 0; j <n; j ++) <br/> matrix [I] [J] = 0; <br/> matrix [0] [(n + 1)/2- 1] = 1; <br/> int x = 0, y = (n + 1)/2-1); <br/> // count: remember the inserted data <br/> for (int count = 2; count <= N * n; count ++) <br/> while (true) <br/>{< br/> // first X-1 y + 1 <br/> X-= 1; <br/> Y + = 1; </P> <p> // determine whether the limit can be inserted <br/> while (true) <br/> {// judge whether the limit exceeded the border according to the statement, until a region does not cross the border. <br/> // determines whether the region exceeded the border: <br/> // the upper bound is greater than x <0, move to the bottom x = x + N, and y remains unchanged; Continue <br/> If (x <0) <br/>{< br/> X = x + N; <br/> continue; <br/>}< br/> // The more the right corner y> = N, then Y = y-n, x does not change; Continue <Br/> If (Y> = N) <br/> {<br/> Y = y-n; <br/> continue; <br/>}</P> <p> // judge whether the specified position already exists, until a blank space is found <br/> // if there is a number, move to X = x + 2; y = Y-1; continue <br/> If (Y <0) {Y = Y + N; Continue ;}< br/> If (Matrix [x] [Y]! = 0) <br/>{< br/> X = x + 2; y = Y-1; <br/> If (x> = N) {x = x-N; Continue ;}< br/> If (Y <0) {Y = Y + N; Continue ;}< br/> continue; <br/>}< br/> break; <br/>}< br/> // allocate the previous Count value to the selected blank space. <br/> matrix [x] [Y] = count; <br/> break; <br/>}< br/> Public static void print () <br/> {<br/> for (INT I = 0; I <n; I ++) <br/> {<br/> for (Int J = 0; j <n; j ++) <br/>{< br/> // system. out. println (Matrix [I] [J]); <br/> system. out. print (Matrix [I] [J]); <br/> system. out. print ("_"); <br/>}< br/> system. out. print ("/N"); <br/>}</P> <p> Public static void main (string [] ARGs) <br/> {// manually enter the value of N and ensure it is an odd number <br/> n = 11; <br/> magic_squre_odd_generate (); <br/> Print (); <br/>}< br/>
4.2 generation of 4 m Magic Square
/** <Br/> * @ Author: Contribute to Wikipedia according GNU <br/> * @ description: used to create level 4 magic party <br/> */<br/> public class magic_square_4m {<br/>/** <br/> * @ Param ARGs <br/> */<br/> static int matrix [] []; <br/> static int N; <br/> static void magic_squre_4m_generate () <br/>{< br/> // initialize matrix <br/> matrix = new int [N] [N]; <br/> // arrange the positions in the matrix in order of numbers <br/> int ini = 0; <br/> for (INT I = 0; I <N; I ++) <B R/> for (Int J = 0; j <n; j ++) <br/> matrix [I] [J] = ++ ini; </P> <p> // print the pre-check result. <br/> system. out. println ("look before the call:"); <br/> Print (); </P> <p> // then (only traverse the number in the upper right corner) <br/> for (INT I = 0; I <n; I ++) <br/> for (Int J = I + 1; j <n; j ++) <br/>{< br/> If (I! = J) & (I + J )! = (N-1) <br/> {// The number of pairs that are not on the diagonal line of the primary payment. About the center reconciliation <br/> int temp; <br/> temp = matrix [I] [J]; <br/> matrix [I] [J] = matrix [n-1-I] [n-1-J]; <br/> matrix [n-1-I] [n-1-J] = temp; <br/>}</P> <p> Public static void print () <br/> {<br/> for (INT I = 0; I <n; I ++) <br/> {<br/> for (Int J = 0; j <n; j ++) <br/>{< br/> system. out. print (Matrix [I] [J]); <br/> system. out. print ("_"); <br/>}< br/> system. out. print ("/N"); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> // here, manually set the value of N to 4, which can only be set to 4, because only Level 4 magic party is required <br/> N = 4; <br/> magic_squre_4m_generate (); <br/> system. out. println ("after the call:"); <br/> Print (); <br/>}< br/>