Iterative Compounding of Square Matrices to Generate Large-Order Magic Squares Wayne Chan1 and Peter Loly2 1 Centre for Earth Observation Science Department of Geography 2 Department of Physics and Astronomy University of Manitoba Winnipeg, Manitoba Canada R3T 2N2 August 15, 2004 Abstract An old idea for compounding magic squares is updated with modern computational methods in order to facilitate the generation of very largeorder numerical matrices, including magic squares. A “base” square is incremented by multiples of the sum of its elements to provide subsquares of another “frame” square. If the base and frame are di¤erent, a second compound square results from their interchange, while identical squares may be said to generate “fractal” squares, i.e. self-similar on di¤erent scales. If both squares are magic (all rows, columns and the main diagonals having the same sum), then the compound square is also magic. Extremely large squares can be obtained easily by iteratively repeating the compounding process, either with the square itself, its base or frame square, or with yet another di¤erent square.
1
De…nitions
We begin with a list of de…nitions [1] to facilitate the present study of magic squares and some closely related issues: A classical magic square [2] is the set of n2 (n > 2) sequential integers 1::n2 arranged in the form of an n n array whose rows, columns and diagonals have the same sum (constant): Cn =
1
n 2 n +1 2
(1)
The smallest (lowest order) magic square occurs for the n = 3 case and is unique apart from rotations and re‡ections: 4 3 8
LS =
9 5 1
2 7 6
(2)
Non-classical magic squares use a non-sequential set of integers. In this paper all the squares of interest use the classical sequence 1::n2 , so that we drop the classical descriptor. A semi-magic square [2] has the same row and column sums as a magic square, but one or both diagonals do not share that sum. SM below may be obtained from LS in (2) by moving its …rst column to be the last column: 9 2 4 SM = 5 7 3 (3) 1 6 8 A pandiagonal (non-magic) square (Bellew’s “o¤-diagonals” [1]) is the set of n2 (n > 1) sequential integers 1::n2 arranged in the form of an n n array [4] and the sum Cn for its main diagonals and pandiagonals, but not for its rows and columns. Pandiagonals are often referred to as split or broken diagonals, meaning a diagonal line of numbers parallel to the main or o¤-diagonal that starts in the top row and continues diagonally downward until reaching the side of the square. The split diagonal is then completed by wrapping around to the other side of the square and continuing in the next row. A simple example of a pandiagonal non-magic square is the serial [5] square S3 of the row-by-row sequence 1::32 :
S3 =
1 (2) 4 5 (7) 8
3 (6) 9
(4)
which has one of its pandiagonals, 2; 6; 7, emphasized by parentheses. It may help to tile a copy of S3 to an edge of itself to see the continuity of the n element line, or even to wrap the square onto a torus [6] to join all opposite edges for the same e¤ect. All serial squares are pandiagonal. Another description gives pandiagonals as the combination of any two parallel segments on each side of the main diagonals which total n elements. A pandiagonal magic square [2] combines the magic and pandiagonal properties. Also called Nasik squares.
2
Most-perfect pandiagonal magic squares have the additional property that all 2 2 subsquares have the same sum [7], including those that run over the edges when tiled or when wrapped over a torus [6]. The augmentation square En is an n n array of 1’s multiplied by n2 . While all of its rows, columns, and diagonals have the same sum, n, it is regarded as trivial in the context of magic squares. A compound square of an n n frame matrix F with an m m base matrix B consists of an n n array of m m submatrices, where the (i; j)th subsquare of the compound square is given by B + (Fi;j
1) Em
(5)
where F and B may be the same or di¤erent, and each may be any type of square (magic, pandiagonal, ...). Also called a composition or composite square [2].
2
Introduction
The ancient Chinese Lo-shu 3 3 (order three) magic square 2 is unique, apart from rotations and re‡ections, and has been a source of wonder and utility for some two thousand years [3]. About a thousand years ago [3] 4 4 magic squares were reported, and at some later date 4 4 pandiagonal magic squares. About a century ago most-perfect pandiagonal magic squares were discovered, the smallest of which are 4 4. For example M :
M=
14 11 5 4
7 12 2 13 16 3 9 6
1 8 10 15
(6)
In this 4 4 case all the 2 2 subsquares have the same sum as the magic constant, C4 = 34. In 1693 Frénicle de Bessy listed the complete set of 880 distinct 4 4 magic squares [9], of which a subset of 48 squares are pandiagonal and also mostperfect [7]. The pandiagonal property is best seen by tiling a copy of M to one side (or edge) of itself: 14 11 5 4
(7) 2 16 9
12 1 (13) 8 3 [(10)] [6] 15
14 [7] [11] 2 5 16 (4) 9
12 1 13 8 3 10 6 15
(7)
In (7) all the lines parallel to the main diagonals also have the same sum (C4 ), e.g. 7 + 13 + 10 + 4 = 34, as emphasized by the elements in round brackets, and 6 + 10 + 11 + 7 = 34, as emphasized by the elements in square brackets. 3
There has always been an interest in larger magic squares, frequently in the context of recreational mathematics, but also more recently as serious studies [7]. However as the size of magic squares increases, the number of distinct magic squares grows even faster. There are 275; 305; 224 distinct 5 5 magic squares [1], but no exact count for the sixth-order squares. However a recent Monte Carlo simulated annealing computation [8] gives an estimate of (0:17745 0:00016) 1020 distinct sixth order magic squares. Recently Ollerenshaw and Brée [7] achieved a remarkable breakthrough by …nding a formula for counting a special class of magic squares, the so-called pandiagonal most-perfect ones which have doubly-even dimensions (divisible by 4) for dimensions 4; 8; 12; ::. We were also aware of the state of world records for the size of magic squares, with Louis Caya reaching 3001 3001 in 1994 [10][11]. The “rules” for worldrecord attempts (Rekord-Klub Saxonia [10][11]) require that the magic square be written out by hand or be printed by a computer, and be veri…ed independently. A revision of these rules is prompted by our construction of a dramatically larger 12; 544 12; 544 magic square. In the literature there are algorithms for constructing magic squares of oddorder, of singly-even order (divisible by 2) or doubly-even order (divisible by 4), but generally these algorithms result in a single square. Whilst pursuing some other work on pandiagonal (non-magic) number squares [4][5], one of us (Loly) conceived the idea of constructing larger ones from smaller ones in a “fractal” manner [12], i.e. having the same pattern on di¤erent scales. For magic squares it was immediately clear that such a construction would retain the magic properties of the row, column and main diagonal sums. Subsequently we found earlier references to the compounding of magic squares [13][14][15][16]. Cammann [17][3] found compound magic squares recorded in China by 1275. The present article breaks new ground in providing modern computational tools for the generation of very large numerical squares, magic or otherwise.
3
Compound Squares
In general, two di¤erent squares can be used in the compounding process. We consider a base square B of dimensions m m and a frame square F of dimensions n n, from which a compound square of dimension nm nm and elements 2 from 1 to (m n) is generated by replacing an element of the frame matrix by the base matrix (now a subsquare) whose elements are incremented progressively by m2 according to the elements of the frame square. When the base and frame squares are di¤erent, a second compound square results from their interchange, while identical squares may be said to generate “fractal” squares, i.e. self-similar on di¤erent scales.
4
3.1
How Compounding Works
Consider a simple 2 2 base square L of the sequence 1; 2; 3; 4 …lled out rowby-row in the usual way for matrices [5]: L=
1 3
2 4
(8)
We chose the 2 2 square L in (8) for its pandiagonality, knowing that the smallest (3 3 ) magic square, LS in (2), is not pandiagonal, in order to draw attention to the pandiagonal property which is of considerable interest in the magic square context for 4 4 and larger squares. Now enlarge (8) by making a 2 2 “supersquare” or frame matrix of four copies which are augmented according to the layout of the original base square to use the sequence 1::16 in the following manner: P = where E2 is the 2
L L + 2E2
L + E2 L + 3E2
(9)
2 augmentation matrix: 1 1
E2 = 2 2
1 1
=
4 4
4 4
This results in: P =
1 3 9 11
2 5 6 4 7 8 10 13 14 12 15 16
(10)
Observe that the pandiagonals of P have the same sum, C4 = 34, as did M in (6). The invariance of pandiagonal line sums is similar to that of the row, column and main diagonal sums of magic squares, some of which are also pandiagonal.
3.2
Compound Magic Squares
It is easy enough to do the compounding of the 3 3 magic square to a 9 9 magic square [1], of a 3 3 magic square with a 4 4 magic square in two ways to make a pair of 12 12 magic squares [15], and so on. However it is clear that the process can be continued inde…nitely [15] so that really large magic squares can easily be made using a computer from any compounding of smaller magic squares. At this point we discovered that the smaller ones had been given by Andrews in 1917 [13], though he credits the idea to a Professor Hermann Schubert [16]. Andrews clearly understood that inde…nitely large compound squares were possible, something not explicitly mentioned by the other references that we have found. Andrews shows an 18 18 example. Allan Adler [18] has given a clear explanation of the compounding idea in some web pages for a mathematics education project at Swarthmore [19]. 5
3.3
Example of Compounding a Pandiagonal Magic Square
As a further illustration of compounding we compound the pandiagonal 4 4 magic square, M , with itself (“squaring”). The original base M subsquare (6) appears in the upper right 4 4 subsquare below:
222 219 213 212 174 171 165 164 78 75 69 68 62 59 53 52
215 210 224 217 167 162 176 169 71 66 80 73 55 50 64 57
220 221 211 214 172 173 163 166 76 77 67 70 60 61 51 54
209 216 218 223 161 168 170 175 65 72 74 79 49 56 58 63
110 103 107 98 101 112 100 105 30 23 27 18 21 32 20 25 254 247 251 242 245 256 244 249 142 135 139 130 133 144 132 137
108 109 99 102 28 29 19 22 252 253 243 246 140 141 131 134
97 104 106 111 17 24 26 31 241 248 250 255 129 136 138 143
190 183 188 187 178 189 181 192 179 180 185 182 206 199 204 203 194 205 197 208 195 196 201 198 46 39 44 43 34 45 37 48 35 36 41 38 94 87 92 91 82 93 85 96 83 84 89 86
177 184 186 191 193 200 202 207 33 40 42 47 81 88 90 95
14 11 5 4 126 123 117 116 158 155 149 148 238 235 229 228
7 2 16 9 119 114 128 121 151 146 160 153 231 226 240 233
(11) While it is easy enough to do the arithmetic for squaring LS in 2 and L in 8, it begins to become tedious to multiply LS and M , or square M , so that we wrote computer programs to automate the task, as well as to verify the results for other squares. We have also used procedures written in Maple [5][20] to check that the result is magic and pandiagonal. Algorithms as fragments of Maple code are given in the Appendix. The magic property (rows, columns, and main diagonals) is expected in view of the construction. However the preservation of the pandiagonal property is not obvious, but it is clearly not most-perfect [7]. The latter is a local property, whereas the pandiagonal line sums are global in the sense that they span the compound square.
4
Preservation of Magic and Pandiagonal Invariances under Compounding
First examine the compounding of the 3 3 magic square LS with itself by using the augmentation square E3 for the following supersquare of subsquares: LS + 3E3 LS + 2E3 LS + 7E3
LS + 8E3 LS + 4E3 LS
6
LS + E3 LS + 6E3 LS + 5E3
(12)
12 13 3 6 124 125 115 118 156 157 147 150 236 237 227 230
1 8 10 15 113 120 122 127 145 152 154 159 225 232 234 239
Now all the rows and columns, as well as the main diagonals, in this 9 9 square have 12E3 in addition to 3LS from the underlying base 3 3 squares, so that the magic square property is preserved. Clearly this argument applies to any sized compound magic square. Semi-magic squares which don’t have main diagonals with the magic sum also compound appropriately. Bellew [1] has a very interesting analysis of compounding magic squares in which he has a similar approach to the row, column and main diagonal sums based on work by Andrews [13]. He also shows how very large numbers of variants of the compound squares can be generated and counted when the subsquares are independently rotated or inverted, bringing into play a factor of 8 each time, for a multiplicity of order 8^9 = 134; 217; 728 in the 3 3 case, and correspondingly larger for larger compound squares. The retention of the pandiagonal property on compounding is more di¢ cult to understand. To this end take the purely pandiagonal (non-magic) 4 4 matrix [5], P in (10), and compound it with itself using the 4 4 augmenation matrix E4 to make the supersquare: P P + 2E4 P + 8E4 P + 10E4
P + E4 P + 3E4 P + 9E4 P + 11E4
P + 4E4 P + 6E4 P + 12E4 P + 14E4
P + 5E4 P + 7E4 P + 13E4 P + 15E4
All 8 pandiagonals of the supersquare now contain 30E4 and 4P , which means that it is clear that for the 16 16 compound square, the 8 pandiagonals which are main diagonals in the subsquares, 2 of them being the main diagonals of the compound square, will have the same sum. However the other 24 pandiagonals pass through 8 rather than 4 subsquares as a pair of segments of 2 elements, or an alternation of 1 and 3 elements. Use the 16 16 square in (11) to locate these patterns, and if needed, refer to (7) for the tiling of a 4 4 square which has these pairs of segments indicated by brackets. In each pair of adjacent subsquares these pairs of segments, composed of 4 elements, have their right (top) segment elements incremented by the 40 s in one pandiagonal of the supersquares, and their left (bottom) segment elements incremented by the 40 s in the next rightmost pandiagonal of the compound square. Since each pandiagonal of the supersquare has the same number of E4 ’s, the pandiagonal property is clear. This argument applies to any pair of pandiagonal squares, and not just the compounding of a square with itself. The magic and pandiagonal conclusions combine to establish the pandiagonality of compounded pandiagonal magic squares. Other types of squares, e.g. pandiagonal with either magic rows or columns, also compound appropriately. The compounding process retains the lowest common denominator of magic properties of the two magic squares (base and frame) used in the process. That is, if one square is pandiagonal and the other is not, then the …nal compound square is a non-pandiagonal magic square. However, if both the base and frame squares are pandiagonal, then the …nal square is also pandiagonal.
7
5
A New World Record Magic Square?
The Rekord-Klub Saxonia, a world-recordholders club in Germany [10][11], gives the following history of previous world records in magic square generation: Size 105 105 501 501 897 897 1000 1000 2001 2001 2121 2121 3001 3001
Name R. Suntag (USA) G. Lenz (Germany) F. Tast and U. Schmidt (Germany) C. Schaller (Germany) S. Paulus, R. Billing, J. Sutter (Germany) R. Laue (Germany) L. Caya (Canada)
Year 1975 1979 1987 1988 1989 1991 1994
In March 2001 we used a combination of Microsoft Excel and IDL (Interactive Data Language) [21] to iteratively compound pandiagonal magic squares of dimensions 64 and 196 in order to make what we believe to be the largest pandiagonal magic square at 12; 544 12; 544. This size was chosen to beat Caya’s 1994 record 3001 3001 magic square [10][11], and to push the limits by making the largest square that we could write to a CD-ROM disk, some 620 megabytes. Clearly one could write an even larger square of many gigabytes onto a DVD. Around the time we generated our large square, we saw a Web announcement by Bogdan Golunski [22] claiming a new uno¢ cial record at 10; 021 10; 021, which our square still surpasses.. He presented just 10 columns from this square on his webpage [22]. For our construction of a new world record magic square we chose Euler’s 1779 7 7 pandiagonal magic square [7]: 41 26 11 29 21 44 3
8 17 4 23 33 49 35 48 36 10 18 2 16 5 28 34 43 38 45 39 9 19 7 27 6 22 31 46 37 12 40 14 20 1 24 32 25 30 47 42 13 15
(13)
and a 4 4 pandiagonal most-perfect magic square [7], which is just a permutation of M in (6): 1 12 15 6 10 3 8 13
7 14 9 4 16 5 2 11
(14)
Using a computer program written in Visual Basic for Microsoft Excel, the 4 4 square was compounded twice with itself to make …rst a 16 16 pandiagonal 8
magic square and then a 64 64 pandiagonal magic square, which was later used as the frame matrix in the …nal assault on the record. Meanwhile the 7 7 square was …rst compounded with itself to make a pandiagonal 49 49, and then with the 4 4 to make a 196 196 pandiagonal magic square, which would be used as the base matrix in the …nal compounding. Since the size of the matrices was by now too large to proceed further with the Excel spreadsheet, or with the Maple code being used concurrently by Loly, Chan wrote a row-by row compounding program in IDL [21], employing a formula similar to the last code fragment given in the Appendix. The …nal result was then written to disk, taking approximately 620 MB of storage. The sizes of the matrices compounded were chosen to create a …nal compound square that would just …t onto a CD-ROM. We thought that it no longer made sense to try and print the square out on paper, as Rekord-Klub Saxonia rules [10][11] would require, largely due to the enormous amount of paper (approximately half-a-million A4 pages with a standard font), let alone wearable printer parts, that would be required to print out some 157 million whole numbers ranging from 1 to 157; 351; 936, whose magic sum is nearly one trillion (986; 911; 348; 864)! We believe that a revision of the rules for magic square records is prompted by our construction of a dramatically larger magic square. Perhaps a separate world record category can be created for the generation of magic squares without the requirement of printing on paper. However, even that begins to seem rather pointless, given the ease of construction demonstrated in this paper. Due to the extremely large size of our square, we decided not to try to display the actual numbers in the square. Instead, a visual representation of the record square has been constructed by using a 24-bit colour code to compress the range of integers (1::157; 351; 936). We have placed a small version (200 200 pixels) of this image on a website [home.cc.umanitoba.ca/~loly/smallmath.jpg]. Even though these images smooth over the massive amount of underlying data, they look rather like a quasi-random (or fractal) “tartan”, showing clearly several iterations of 4 4 substructure. As such they may have some artistic merit. Explicit veri…cation of the magic properties is a considerable task, involving some 100; 352 line sums to con…rm that we have a pandiagonal magic square. Chan has checked all the row and column sums with Microsft Excel, and Loly developed the Maple code in the Appendix to compute the pandiagonal sums. Since a selection of the pandiagonals gave the same sum, the argument in section 4 was developed to obtain a general conclusion for all line sums.
6
Conclusions
The process of compounding magic squares has been automated to make very large magic squares, and at the same time generalized to related numerical squares, which include semi-magic squares, pandiagonal squares of various types, and even random squares. Global properties of constant line sums have been established for appropriate varieties. In combination with Bellew’s study [1] of
9
very large numbers of magic squares resulting from rotation and re‡ection of individual subsquares in small compound magic squares (9 9, 16 16, ...), it is clear that extremely large numbers of compound squares can easily be generated.
7
Acknowledgments
We thank Marcus Steeds and John Hendricks for many communications on magic square topics, and also the referee for helpful suggestions.
References [1] Bellew, James (1997). “Counting the Number of Compound and Nasik Magic Squares”, Mathematics Today, August 1997, 111–118. [2] Heinz, Harvey D. and Hendricks, John R. (2000) Magic Squares Lexicon: Illustrated, published by Heinz as demand dictates: 15450 92A Avenue, Surrey, BC, V3R 9B1, Canada,
[email protected] [ISBN 0-9687985-0-0]. Available from John R. Hendricks, #308, 151 St. Andrews St., Victoria, B.C., V8V 2M9, Canada. [3] Swetz, Frank J. (2002) Legacy of the Luoshu - The 4000 Year Search for the Meaning of the Magic Square of Order Three. Chicago: Open Court. [4] Loly, P. D. (2002) A purely pandiagonal 4 4 square and the Myers-Briggs type Table, J. Rec. Math. [accepted 1998 - to be published in Vol. 31, No. 1]. [5] Loly, P. D. and Steeds, Marcus Pandiagonal Non-Magic Squares (preprint 2001). This paper is a considerable development of Loly’s original discovery of pandiagonal non-magic squares [4]. [6] Gardner, Martin. (1961, 1987) The Second Scienti…c American Book of Mathematical Puzzles and Diversions. The University of Chicago Press. A 4 4 magic square wrapped on a torus is shown on p. 136. [7] Ollerenshaw, Kathleen and Brée, David S. (1998) Most-perfect pandiagonal magic squares: their construction and enumeration. Southend-on-Sea, U.K.: The Institute of Mathematics and its Applications. N.B. We added a “1” to all elements in their magic squares so that they begin with “1” rather than the “0” which is often convenient for mathematical reasons. In lieu of the book until the reader can get hold of a copy, try http:// www.magic-squares.com or www.most-perfect.com, where a short article summarizes their work. [8] K. Pinn and C. Wieczerkowski, Number of magic Squares from Parallel Tempering Monte Carlo. International Journal of Modern Physics C, Vol. 9, No. 4, 541-546 (1998). 10
[9] Ollerenshaw, Kathleen and Bondi, Hermann. (1982) Magic squares of order four. Philosophical Transactions of the Royal Society of London, A306, 443532. [10] Rekord-Klub Saxonia: www.recordholders.org/en/records/magic.html [11] Pickover, Cli¤ord. (2002) The Zen of Magic Squares, Circles, and Stars An Exhibition of Surprising Structures across Dimensions. Princeton University Press. [12] Mandelbrot, B. The Fractal geometry of Nature, Freeman, New York (1982). [13] Andrews, W. S. (1917) Magic squares and cubes, 2nd edition Chicago: Open Court Publishing Company [Republished unaltered (with chapters by other writers) by Dover Publications, New York (1960).] p. 44–46. Andrews notes two ways of getting a 12 12, his next is not 15 15, but 16 16 and then 18 18. He does indicate that inde…nitely larger squares can be made. [14] Gullberg, Jan. (1996) Mathematics from the Birth of Numbers, New York, London: W. W. Norton & Company (1996). p. 210 - quite explicit for the 9 9 case. [15] Hendricks, John R. (2000) personal communication. See also Hendricks, John R. (1992) The Magic Square Course, 2nd edition, privately printed, p. 76–78 show the two 12 12 compound squares. [16] Schubert, Hermann. (1903) Mathematical Essays and Recreations. Chicago: Open Court. [17] Cammann, S. (1962) Old Chinese Magic Squares. Sinologica 7, 14-53. [18] Adler, Allan: see Suzanne Alejandre’s web site [19]. [19] Alejandre, Suzanne: http://forum.swarthmore.edu/alejandre/magic.square/adler/ The 3 3 iterated once is found on page: adler.squaresq2.html; and a 12 12 with base 3 3 and frame 4 4 is found on page: adler1.html [20] Char, B. W. (1991) Maple V Library Reference Manual, Springer-Verlag, New York. [21] IDL: Interactive Data Language: www.rsinc.com. [22] Golunski, Bogdan (2001): http://www.golunski.de/
11
1 1.1
Appendix Algorithms
We have written Maple r code to automatically generate compound squares. Maple has a “copyinto” function [20] that is ideally suited to formulating the compounding code. The function copyinto(A; B; i; j) copies matrix A into matrix B, with the upper-left corner of A located at element B[i; j] of matrix B. If B and F are the base and frame squares respectively, and nf is the frame dimension, nb the base dimension, E is the augmentation matrix with the dimension of the base …lled with “nb nb’s”, and COM P OU N D is the (initially blank) receiving matrix, then the compounding code is simply: f or j to nf do f or i to nf do copyinto(B + (F [i; j] 1) E; COM P OU N D; 1 + (nb i nb); 1 + (nb j od od : 1.1.1
nb))
Computing Speci…c Vectors and Elements
For generating very large magic squares, and for veri…cation purposes, it is sometimes useful to generate certain elements of the compound square without having to construct the whole square. The following Maple code fragments generate the pandiagonal vectors of a compound square, and a given element of the compound square. 1.1.2
Computing the Pandiagonal Vectors
The pandiagonal vectors, P D, indexed by nD = 1; 2; :: from the main diagonal (nD = 1) of the square, and nbnf = nb nf ) which slope down to the right can be formed by: f or k to nbnf do P D[k] := B[modp(k 1; nb) + 1; modp((k + nD 2); nb) + 1] +nb^2 F [ceil(k=nb); modp(trunc((k + nD 2)=nb); nf ) + 1] nb^2 od : where we have used B for the base matrix and F for frame matrix. Then the pandiagonal vectors, DP , which slope down to the left are given by: f or k to nbnf do DP [k] := B[modp(k 1; nb) + 1; modp((nbnf k + nD 1); nb) + 1] +nb^2 F [ceil(k=nb); modp(trunc((nbnf k + nD 1)=nb); nf ) + 1] 12
nb^2 od : 1.1.3
Computing a General Element
A general element (ir; ic) of the compound square is available from: B[modp(ir 1; nb) + 1; modp((ic 1); nb) + 1] + nb^2 F [modp(trunc((ir 1)=nb); nf ) + 1; modp(trunc((ic 1)=nb); nf ) + 1] nb^2; This last code fragment is useful for constructing individual rows of a very large square, without the need to generate and store the whole matrix.
13