(1) When n is odd, it is the simplest.
(1) Put 1 in the middle column of the first row;
(2) Numbers from 2 to n×n shall be stored according to the following rules:
Go 45, for example, turn right.
The number of rows stored in each number is 1 less than the previous number, and the number of columns is 1.
(3) If the range of rows and columns is beyond the range of matrix, line feed is performed.
For example, 1 is in line 1, then put 2 in the bottom line and add1to the number of columns;
(4) If there is a number at the position determined according to the above rules, or the last number is the nth column of 1 line,
Put the next number below the previous one.
(2) When n is a multiple of 4,
Symmetry element exchange method is adopted.
First, the numbers 1 to n×n are filled into the matrix from top to bottom and from left to right.
Then, the number of positions on two diagonal lines in all 4×4 submatrixes of the square matrix is aligned with respect to the center of the square matrix.
Call switching, that is, a(i, j) is exchanged with a(n- 1-i, n- 1-j), and the numbers of all other positions remain unchanged.
(or keep the diagonal unchanged, and other positions can be symmetrically interchanged. )
(3) When n is other even numbers.
When n is an even number that is not a multiple of 4 (that is, 4n+2 shape), the large square matrix is first decomposed into four odd-numbered (2m+ 1 order) sub-squares.
Assign values to the four sub-matrices after decomposition according to the magic square above.
The upper left subarray is the smallest (i), the lower right subarray is the smallest (i+v), the lower left subarray is the largest (i+3v) and the upper right subarray is the largest (i+2v).
That is to say, the corresponding elements of the four submatrixes differ by v, where v = n * n/4.
The arrangement of the four sub-matrices from small to large is ① ③.
④ ②
Then the corresponding elements are exchanged: a(i, j) and a(i+u, j) are exchanged in the same column (j).
A(t- 1, 0) and A (T+U- 1, 0); A(t- 1, t- 1) and a(t+u- 1, t- 1) exchange two pairs of elements.
Where u=n/2, t=(n+2)/4 The above exchange makes the sum of elements in each row and column equal to the sum of elements in two diagonals.
C language implementation
# contains "stdio.h"
# contains "math.h"
Int a is a step to the left. The horse stance just look can be expressed as 2X+Y, {x ∈ {,}, y ∈ {[0, 1], [0, 1]} {y ∈ {,}, and the corresponding jump of X∈{[2X+Y can The above is an x jump. The Rubik's Cube generated by Mafa is the Devil's Cube.
Generating even-order magic squares by Hire method
Consider the magic square of order n as a matrix, and write it as a, and the numbers in the grid of row I and column J are written as a(i, j). Fill in 1, 2,3, ..., n on the two diagonal lines of A, and then fill in 1, 2,3, ..., n, so that the sum of the numbers in each row and column is n*(n+ 1)/2. The filling method is: line 1 is filled from n to 1, and line 2 to n/2 is filled from 1 (line 2, line 1, line 2, column n 1), and line 2, line n/2+/kloc. The following is the sixth-order filling method:
1 5 4 3 2 6
6 2 3 4 5 1
1 2 3 4 5 6
6 5 3 4 2 1
6 2 4 3 5 1
1 5 4 3 2 6
The following is the eighth-order filling method (after transposition):
1 8 1 1 8 8 8 1
7 2 2 2 7 7 2 7
6 3 3 3 6 3 6 6
5 4 4 4 4 5 5 5
4 5 5 5 5 4 4 4
3 6 6 6 3 6 3 3
2 7 7 7 2 2 7 2
8 1 8 8 1 1 1 8
Calculate all the numbers on a according to the following algorithm to get b, where b (i, j) = n× (a (i, j)- 1). Then at+b is the target Rubik's cube.
(AT is a transposed matrix). The magic square of order 8 generated by the lease method is as follows:
1 63 6 5 60 59 58 8
56 10 1 1 12 53 54 15 49
4 1 18 19 20 45 22 47 48
33 26 27 28 29 38 39 40
32 39 38 36 37 27 26 25
24 47 43 45 20 46 18 17
16 50 54 53 12 1 1 55 9
57 7 62 6 1 4 3 2 64
Strachey method for generating simple even magic squares
The simple even magic square of order n is expressed as the magic square of order 4m+2. Divide into four parts and become four magic squares of 2m+ 1 order as shown in the figure below.
A c
D B
A from 1 to 2m+ 1 to fill (2m+ 1) the second-order magic square; B fill the magic square of order 2m+ 1 with (2m+ 1) 2 to 2*(2m+ 1)2; C Fill the magic square of order 2m+ 1 with 2 * (2m+ 1) 2 to 3*(2m+ 1)2; D fill the magic square of order 2m+ 1 with 3 * (2m+ 1) 2 to 4*(2m+ 1)2; Take m cells in the middle row of A, where 1 cells are in the middle of the row, M- 1 cells are arbitrary, and the left edges of other rows take m columns, which are interchanged with the corresponding cells of D; The m- 1 column near the right side of b and c is interchanged. The sixth-order magic square generated by Strachey method is as follows:
35 1 6 26 19 24
3 32 7 2 1 23 25
3 1 9 2 22 27 20
8 28 33 17 10 15
30 5 34 12 14 16
4 36 29 13 18 1 1
Generating even magic squares by spring method
The n-order dichotomy magic square is expressed as the 4m-order magic square. Consider the magic square of order n as a matrix, and write it as a, and the numbers in the grid of row I and column J are written as a(i, j).