Written in 2012.10:
Originally this is rectification that "C Program Design (fourth edition)" of a after class exercises, just start to do when to find the best algorithm on the Internet, the results found that odd and double even (4 multiples) of the case algorithm is relatively simple, But a single even number ( a multiple of 2 but not a multiple of 4) has been unable to find a definite algorithm, even the Baidu Encyclopedia of the explanation of the problem is " because the non-four multiple approach is quite complex, Only a multiple of four is introduced here, and even the answer to Rectification's book is a disguised limit of n can only be odd (the title is not stated). After extensive search for information, found a paper by a professor and graduate students from the College of Information Science and engineering, South Central University, introduced an arbitrary order magic square algorithm, because the online data is incomplete, there are many print errors, has not been able to understand the paper introduced by the method (paper address attached, interested students can see). At that time did not make a single even-numbered situation directly handed over the homework.
Today again to do this unfinished topic, in the Net library access to the information, and then inadvertently found using "magic square" as the key word to search out a lot of algorithms (profound explanation of a problem ...), after detailed reading, found that single even-numbered case algorithm is not very complex, online cattle people are still a lot of it, So he tried to finish the problem.
Back to the theme, Arbitrary order magic Square (Magic squares) algorithm (here, each case only introduces a more common one, the other algorithm after the address, interested students can see):
1, odd situation: Robert Law, also known as stair law.
Fill in this way:
put or the smallest number ) put it in the first row in the middle; The remaining n*n-1 number:
(1)
(2)
(3)
(4) (4)
This is always a "top-right" direction, like climbing stairs.
2, double even case: diagonal Complementary method:
First describe a definition:
Complementarity: If the sum of two digits equals the sum of the magic square maximum and minimum number, i.e. n*n+1, is called complementary.
Let 's take a look at the 4-Order magic Square: Fill in the numbers from left to right, from top to bottom:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Replace the number on the diagonal (including the main diagonal and the second diagonal) with the number that complements it.
Over heren*n+1 = 4*4+1 =;
Put1Replace17-1 =; put6Replace17-6 = One; put OneReplace17-11 = 6... After the change is a four-order magic square.
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
So, forn=4kOrder Magic Square, we first fill in the numbers in order. After writing, press4*4divide it intoK*ka square. BecauseNis a4can be used in multiples of4*4the small square partition. And then put the diagonal of each small square, like making4The method of the order magic square, the number on the diagonal is replaced by the complementary number, it constitutes the magic square. Below is8The practice of order magic Square:
(1)fill in the numbers in order first. Then, press4*4split it up into2*2a small square .
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
(2)The number on the diagonal of each small square, replaced by a number that complements it.
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
3, single even-numbered case: Quadrant symmetric Exchange Method ( this part from the Chi Qiang's home _ Baidu space , slightly modified )
Ton=10as an example,Ten=4x2+2, thenk=2(k= (n-2)/4)
(1) Divide the square intoA,B,C,Dfour quadrants, so each quadrant is definitely an odd order. Using the staircase method, in turnAQuadrant,DQuadrant,BQuadrant,CThe quadrant is filled by odd-order magic squares.
(2) in the middle of a quadrant, the middle of the beginning, according to the left-to-right direction, Mark k lattice. The other lines in the A quadrant mark the leftmost K lattice. Place these lattice, and the number of the relative position of the C Quadrant, the interchange position.
(3) inBthe middle of any row in the quadrant, from right to left, markedk-1column. (Note:6Order magic Square becausek-1=0, so no moreB,DQuadrant for data exchange)and WillBQuadrant to mark out these numbers, andDthe number of quadrants relative to the position is exchanged, and the magic square is formed.
Here is the 6 -Order magic square Fill:6=4x1+2, when K =1
This method looks very troublesome, actually mastered the method is very simple.
The following is my C language version of any order magic square problem, a few simple data passed, because the exercise has not learned the contents of the function, so the program has a lot of duplication and miscellaneous content, if there are errors, please correct:
GitHub Address: Magicsquare
I wrote it in freshman time. Turn it over and tidy it up a bit.
An algorithm of arbitrary order magic Square (magic squares) and implementation of C language