# Magic Squares

 1 15 6 12 8 10 3 13 11 5 16 2 14 4 9 7

Magic squares have been known for millennia. Normally a magic square is a set of consecutive numbers arranged in a square pattern such that every row, column and main diagonal adds to the same number, its magic constant. Pan-magic squares, a 4 by 4 example of which is shown, have additional properties. For pan-magic squares, in addition to the rows, columns, and main diagonals, all broken diagonals also add to the magic constant (34 for this square). The four number sets 8, 15, 9, 2 and 15, 3, 2, 14 are two examples of broken diagonals in the square. An alternate way of describing this property is to wrap around a row or column to the opposite side of the square creating a new square. The diagonals of the new square will have been broken diagonals in the old and vice versa. If the left column in the square above is moved to the right side the new square will have 15, 3, 2, and 14 as an unbroken diagonal and all rows, columns, and diagonals will still add to the magic constant.

This section will discuss a new method to construct pan-magic squares of all possible sizes. The method depends on the combination of two strings of numbers to generate the remainder of the square. The two number strings are called Master Base Lines and are generally the top row and left column of the square being constructed. This page describes the basic construction method with an in depth analysis of the order-4 pan-magic squares. The subsections look at larger order-2p squares, squares of order-3p, prime order squares, and squares of composite number order.

This order-4 square is shown in the traditional representation using a range of numbers from 1 to 16. Often I will show the squares as mathematically generated, sometimes called the analytic representation. That means that the range of numbers would go from 0 to 15. To switch between the two a 1 must be added or subtracted from all the numbers in the magic figure. The description below uses the analytic representation as that is how the squares are generated.

## BASIC CONSTRUCTION CONCEPTS

Construction of the order-4 pan-magic squares has been described in various ways. A method of construction of these squares that can be extended to higher dimensions is discussed below. The concept is based on binary math making it amenable to computer manipulation. The approach is similar to that of Grogono except for the concept of base lines. Many of the concepts discussed using the squares may seem trivial. Many are, when applied to the order-4 square, but they become important when applied to higher order figures. It is far easier to show many of the concepts using the squares. A concept can often be described in more than one way. In some cases more than one description for the same concept is given. Hopefully, the different points of view are useful.

### Base Lines and Base Squares

The basis for construction is the concept of base lines. For the purposes of this discussion a base line for the order-4 magic square is a 4-bit code containing equal numbers of 0's and 1's. The Order-4 Base Square Generator contains two base lines, one for the rows and the other for the columns. These are combined to make a base square. The numbers in the square matrix are generated from an exclusive OR combination of the numbers in the corresponding row and column base lines. The exclusive OR function results in a zero when two ones or two zeros are combined and it results in a 1 when a zero and a one are combined.

The last three bits of the base lines at right may be changed to evaluate potential base lines. The first bit is initially fixed at zero. This makes it easier to see the exclusive OR in the base square since the top row and left column are the base lines and the values at their intersections in the remainder of the square are created using the exclusive OR function.

The sums of all the rows, columns, diagonals and broken diagonals in the Order-4 Base Square Generator must add to 2. Can you modify the base lines in the generator to make all the sums equal 2? Only 0's and 1's can be entered into the base lines.

Hints

1. When both base lines start with zero the top row and left column are the same as the base lines.
2. The two base lines must be different.
3. The base lines must have equal numbers of 0's and 1's.

Solutions can be found on the Answers page.

Only combinations of an A0 base line with either a B0 or B1 base line will give a base square that will add to 2 in all the requisite ways. Base lines and base squares are discussed in more detail below.

### Base Square Magic Constant

 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0
 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1
 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0

The base squares must have all of the addition properties desired in the final square, i.e. all rows, columns, diagonals, etc. add to the magic constant. The magic constant is just 2 for these base squares as demonstrated above. To meet this requirement the base square must have an equal number of 0's and 1's in all the desired groups of four. It is relatively easy to generate order-4 squares having these properties, but there are only four unique squares with a zero in the upper left corner and these are shown at right. Also four have ones in the upper left corner. The zero must go somewhere in the square, however, and assignment to the upper left simplifies construction and is traditional although somewhat arbitrary. The four squares are designated I, II, III, and IV at right. All can be generated above. There is a quick proof that the correct base lines will give correct additions below and a more general mathematical proof for all base lines can be found in Überprüfen.

### Multiplier Shuffling

Numbers from 0 to 9 may be placed in the multiplier boxes. The resulting squares will always add to a magic constant in all the expected ways. Only by having a 1, 2, 4, and 8 combination will the resulting magic square contain consecutive numbers from 0 to 15. For these squares the magic constant will be 30.

Rearranging the multipliers into the 24 possible arrangements rearranges the codes in the Bit Code by Position array correspondingly. For example, the first bit of each code might be exchanged with the third bit to make a new base square. This would be the same as exchanging the multipliers of square I and square III without changing squares II and IV. No matter what the arrangement of the multipliers all 16 of the possible 4-bit code arrangements will be present somewhere in the resulting array. This means that all 24 arrangements will contain all numbers from 0 to 15. Since the addition of valid base squares and/or multiplied valid base squares always results in a square with a valid magic constant, all 24 of the possible magic squares are indeed pan-magic squares. This concept is the basis for the unique cube, unique tesseract, etc. discussed later. A more elegant proof of the above can be found in Überprüfen.

### Translations

The pan-magic squares are still magic when the rows and/or columns are moved from one side to the opposite side. This is often described as a wrap around property. It can also be called a translation. Most magic squares do not have this property. There are16 different positions in the square for any of the values resulting in 16 visually different magic squares. Since there are 24 visually different squares with a zero at the upper left corner, there are 24 x 16 = 384 visually different magic squares with zero's at all possible positions. Pass your mouse over the Translated Square below to translate the zero to all possible positions. This will also translate all other numbers the same vector. Translations are visually different and are sometimes considered to be different squares. Translations of squares and higher figures will be considered the same here since the ability to carry out translations is an inherent property of the pan-magic squares. However, the generators do allow translations to be done.

### Rotations

If the square is rotated 90° clockwise, the resulting square has a visually different appearance as demonstrated by clicking the 'Rotate Clockwise' button at right. When the zero of this rotated square is translated back to the upper left corner, one of the other 24 possible pan-magic squares with a zero at the upper left corner is generated, as seen in the Translated Square at far right. In this case, the base lines for the new magic square will be B0A0, B1A0, A0B1, A0B0. Compare this to the original magic square values of A0B1, A0B0, B1A0, B0A0. For a 90° rotation in either direction the order of the base lines in each pair will reverse. For a clockwise rotation, if the second member of the pair is initially a B0 it will be a B1 after rotation and vice versa. The A0's are unchanged. For a counterclockwise rotation, the first member of the pair will undergo the change. Rotating another 90° clockwise gives the base lines A0B0, A0B1, B0A0, B1A0 and a third rotation gives B1A0, B0A0, A0B0, A0B1. These four squares are visually different, however, they are clearly the same square.

### Inversions (Reflections)

The square can also be inverted through either the center vertical or horizontal axis. This is frequently referred to as reflection to indicate the resulting figure is a mirror image of the starting figure. As with rotation the zero can then be translated back to the upper left corner as can be demonstrated by clicking the horizontal or vertical buttons above. Reflecting through either axis reverses the order of the numbers in the square. Note that in the original magic square that the corners of the square are 0, 11, 6, and 13 in a clockwise direction. After the inversion, this order of numbers goes in a counterclockwise direction. The reflected squares are visually different but they are considered to be the same square. The reflected squares can be rotated into four positions giving the four base line sets, A0B0, A0B1, B1A0, B0A0; B1A0, B0A0, A0B1, A0B0; A0B1, A0B0, B0A0, B1A0; and B0A0, B1A0, A0B0, A0B1. In the vernacular of magic square aficionados rotations and reflections create different aspects of the square. The different aspects are considered to be the same square. The 384 visually different magic squares, that can be made using different base line arrangements and translations, are actually only three distinct order-4 pan-magic squares. Just one of these three is demonstrated above.

### Row and Column Exchanges

Exchanging rows or columns that are spaced two apart in the order-4 pan-magic squares generates a different order-4 pan-magic square. All such exchanges are possible and multiple exchanges can be done in any order. In this type of exchange, the A0 base lines remain unchanged. The B0 base lines become a B1 base line and vice versa. The exchange also exchanges the numbers in the associated master base lines.

A double exchange is also possible. If rows or columns 1 and 2 are exchanged in concert with rows or columns 3 and 4 it gives a different magic square. In this case, it is the same square that would be created by exchanging rows or columns spaced 2 apart. For this type of exchange, the A0 base lines become A1 base lines and the B type base lines are unchanged. Translation of the zero back to the upper left corner after all exchanges will confirm their equality.

These exchanges are trivial in the order-4 square as they just generate one of the known squares. The exchanges become useful in larger figures.

 1 15 4 14 12 6 9 7 13 3 16 2 8 10 5 11

A square generated by the above approach uses the numbers from 0 to 15. To make the more traditional magic square using the numbers from 1 to 16 simply add 1 to all the values in the square. In general the range from 0 to 15 (or 0 to 511 for the cube, etc.) are easier to use for the manipulations carried out in the cube generator. Addition of one to make the traditional magic figure is done last and superficially. The Multiplier Shuffling pan-magic square is converted to a traditional magic square at right. This is the same square seen at the start of this section.

### Base Lines Starting with One

The above magic squares will always be generated with a zero at the upper left corner because the base lines all start with zero. The zero can be moved to any other position without changing the properties of the square by wrapping columns or rows around to the opposite side (translation). If the base lines were required to start at the upper left corner, then translating the zero to a different position would change the base lines describing the square. Some of the new base lines would start with 1's in order to allow a number other than zero on the upper left corner. By checking the Allow Initial 1 in Base Lines checkbox in the Weaving Base Lines section base lines starting with 1 can be observed. The base line A1, 1010, can replace A0, B3 can replace B0, and B2 replace B1for these translations. Use of these additional codes can be confusing, however, as some combinations are redundant. For instance, the combination A0B0 is the same as A1B3. It is easier to work with just the codes starting with 0 and describe translations as a vector relative to a zero in the upper left corner.

## FEATURES

The order-4 pan-magic squares have additional combinations that add to 34. These combinations are not always mentioned in general discussions of pan-magic squares, as they are not requirements for the class. Some are only applicable to the order-4 squares. These combinations are called features. Many features can be determined by careful selection of base lines. That is not easily shown for the order-4 square and is discussed in more detail in Order-2p Squares.

All pairs of numbers 2 apart on a diagonal sum to 17. This is called the complete feature. More generally for squares of order-m = 4x, complete means that all of the number pairs located a (m/2, m/2) vector apart in the square sum to the same value, m2+1 for the traditional square or m2-1 for the analytic square.

All the 2 by 2 squares within the larger square and the corners of each 3 by 3 square add to the magic constant. The former is normally called the compact feature. For the latter I find Aale de Winkel's nomenclature, {2compact3}, useful.

The magic constant is also obtained by adding two consecutive numbers in one column or row to the two numbers that are the continuation of that column or row shifted by two. Two examples are 1, 8, 16, 9 and 14, 10, 3, 7. For the order-4 square, this is actually a consequence of the complete feature but it is not necessarily a consequence for other figures. There are a total of 52 easily described ways that the order-4 magic squares add to 34. The other 34 possible number combinations that add to 34, do not form easily described patterns in the square and those patterns do not translate throughout the square to give other groups that add to the magic constant.

## IN DEPTH DISCUSSION

### Base Lines

There are only three 4-bit codes starting with 0, that have equal numbers of 0's and 1's, 0101, 0011, and 0110. There are also three codes starting with 1 but these are generally not used.

Order-4 base line Order-8 base line Symbol
binarydecimalbinarydecimal
010150101010185A0
001130011001151B0
0110601100110102B1
--0000111115C0
--0001111030C1
--0010110145C2
--0011110060C3
--0100101175C4
--0101101090C5
--01101001105C6
--01111000120C7

Rather than type out 0101, etc. every time one of the base lines is described, a shorthand description has been adopted. The 0101 base line is designated A0. Note that it is just 01 followed by a repetition of 01. The other two base lines start with 00 and 01. The second half of these two base lines is derived by a bit code inversion of the first half. In other words, the second half is made by changing zero's and one's in the first half to one's and zero's in the second. These are the B base lines and are designated B0 and B1 based on the first two bits of the code converted from binary to base ten.

The shorthand description for base lines can easily be generalized to higher orders as shown in the table at right. The letters A, B, C, etc. represent the bit length of a unit of the base line. This bit length is 2 for A, 4 for B, 8 for C, etc., increasing by powers of 2. The number represents the bit code to be placed in the first half of the unit. The other half of the unit is just the inverse of the first half. The unit is repeated until the base line is filled. For order-8, the base line is eight bits long. The order-8 base line's B1 unit starts with 01 for the first half. This is followed by 10, the inverse of 01. The resulting 4-bit unit is then repeated to make the B1 base line 01100110. The C2 unit starts with 0010 the 4-bit representation of 2. A C base line is eight bits long and the second half of the C2 unit is 1101, the inverse of the first four bits. Since the cubes base lines are only eight bits long, the 00101101 unit is not repeated. The C2 unit would be repeated for a 16-bit D base line. A more concise description of the above can be found in Überprüfen.

For the four possible base squares above the alphanumeric codes are B1A0 for square I, B0A0 for II, A0B1 for III, and A0B0 for IV where the first base line is the row code and the second the column.

### Weaving Base Lines

 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1

Pairs of base lines can be combined to make base squares. For purposes of this discussion, a base square is a magic square consisting of just 0's and 1's. Base squares can be generated using an exclusive OR combination of two base lines, one base line for the row and one for the column. For the square at right, the base lines are 0110 for the column and 0101 for the row. The exclusive OR function results in a zero when two ones or two zeros are combined and it results in a 1 when a zero and a one are combined. The second bit of 0110 is a one in the x direction and the fourth bit of 0101 is also a 1, but in the y direction. When combined using the exclusive OR function, the number at their juncture at the fourth row from the top and second column from the left is a zero. When the first digits of both base lines are zero, the base lines are also the top row and left column of the base square. This is because an exclusive OR of the zero with either a 0 or 1 yields the 0 or 1.

 0 1 1 0 0 1 0 1

The resulting base squares have the appearance of simple woven cloth, especially when viewed as repeating units in all directions. For the Woven Base Square at right the row base lines are the red warp threads and the column base lines the green weft threads. The threads extend beyond the square on all sides. To make the Woven Base Square use four red strings for the warp. The first and last strings are down and the middle two up. Slide a green string through the middle and push it to the top (this is upside down for most looms). Reverse the up/down positions of the warp threads and add the next green string. Push next to the first green string. Reverse and add the third string and reverse and add once more to make the square. The resulting cloth appears the same as the Woven Base Square at right where red represents 0's and green 1's. In essence this is the effect of the exclusive OR function. Again, the top row is the warp. A 0 in the left column, the weft, duplicates the top row. A 1 in the left column inverts the associated row.

### Proof of Base Square Magic Constant

Not all exclusive OR combinations of two base lines will give base squares that add up to 2 in all the desired ways. There are only four squares with a 0, or origin, in the upper left corner that add to 2 in all the desired ways. All four contain the A0 base line in one direction and one of the B base lines in the other. If two A0's are combined, the diagonals do not add up properly. Combinations of two B base lines also give erroneous base squares. The reason for this is simple. If two A0 base lines are combined and their intersection at a point in the square is the exclusive OR combination of two 0's, then all adjacent diagonal points are the exclusive OR combination of two 1's. The next position along the diagonal will again be an exclusive OR combination of two 0's. The diagonals will therefore be all 0's. If on the other hand the position is the exclusive OR combination of a 0 and a 1 then the next diagonal position will be an exclusive OR combination of a 1 and a 0 resulting in diagonals of all 1's. The combination of two B0's, two B1's, or a B0 with a B1 will give the same result for some diagonals. For a combination of an A0 base line with one of the B base lines, for any position of the square, the position located a (2,2) vector away will always be its inverse. These two points will always be on a diagonal. Therefore for any two adjacent points on a diagonal the other two points on the diagonal will be their inverse resulting in two 0's and two 1's on that diagonal. This is because the last two bits of the B base lines are the inverse of the first two bits and the last two bits of the A0 base line are the same as the first two bits. A more general mathematical proof for all base lines can be found in Überprüfen.

### Base Square Combinations

There are two rules that are essential for combining base squares. These rules also apply to larger and to multi-dimensional figures as well.

1. If two base squares, with all the appropriate addition properties, are added together, the resulting square will also have the correct addition properties. For the order-4 square case, the added squares will add to four in all the appropriate ways but there will be an uneven distribution of numbers, 4 zeros, 8 ones, and 4 twos, in the resulting intermediate square. This is true for any two order-4 base squares, even the same two.
2. If a base square has all of the appropriate addition properties, then a multiple of that square will also have those properties. In this case, the resulting square will consist of an equal number of zeros and the multiple.

From these rules it is apparent that if one base square is multiplied by two and a second is added without multiplication, the result will have the correct addition properties. In the order-4 square case, using two different base squares will always result in uniform integral distribution, i.e. there will be 4 zeros, 4 ones, 4 twos, and 4 threes in the resulting intermediate square. The uniform integral distribution happens to be true for any pair of the order-4 base squares combined by multiplying one by 2 and adding the second, provided the base squares are different. Uniform integral distribution is not guaranteed for all pair combinations of larger base figures. Combinations that do not yield uniform integral distribution, will not yield valid magic figures. However, they will add to the correct sum in all directions. A mathematical proof of the above can be found in Überprüfen.

One can envisage adding a third base square in two ways. A third base square can be multiplied by four and added to the first two giving numbers from zero to seven. Alternatively, the two base square combination can be doubled and a third base square added. For the order-4 square case, for either approach after addition there will be two of each integer, 0-7, or uniform integral distribution. Again this is always true for the order-4 squares, when three different base squares are used, but not for larger figures. The fourth order-4 base square is added in a similar manner making a magic square with all the numbers from 0 to 15.

### Master Base Lines

 B1 0 1 1 0 B0 0 0 1 1 A0 0 1 0 1 A0 0 1 0 1 0 11 12 7
 A0 0 1 0 1 A0 0 1 0 1 B1 0 1 1 0 B0 0 0 1 1 0 14 3 13

When all four row and column base lines are placed in a table from highest multiplier to lowest multiplier, tables like those at right are generated. The base lines read from left to right in the tables. The columns contain the numbers, written in binary, in the first row or first column of the base squares. The binary numbers in the columns are converted to decimal numbers in red at the bottom. These numbers are the first row and first column of the magic square. By reversing the process, it can be seen that the top row and left column of the magic square has all the information necessary to generate the remainder of the square.

Because of the way the square is generated the other numbers in the square can be determined from an exclusive OR combination of the first row and first column. To do this the zero must be the first number in the row and the column. (Actually the requirement is just that the zero be somewhere in the master base line, but this complicates the explanation.) These are the master base lines of the square. For the default square initially under the Multiplier Shuffling heading, the third number in the top row is 3 and the fourth number in the first column is 7. These are 0011 and 0111 in binary as can also be seen in the Bit Codes by Position table above with multipliers 2, 1, 8, and 4. The exclusive OR combination of the bit pairs at each position is 0100 or 4 in base ten. The number at the juncture of the third column and fourth row is 4. This property is true for higher dimensional figures of the type described here as well. It is not true of all nasik magic figures, i.e. cubes similar to Barnard's1 or other figures that cannot be made with base lines.

### Master Weaving

 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 0 0 1 0 1 1 1 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 1 1 0 1 0 1 1 0 0 1 1 1 1 0 0

The numbers in the master base lines can be woven as was done for the base squares. This is shown at right for the Multiplier Shuffling square. For an order-4 square consider each number to be a bundle of four strings representing the four bit binary equivalent of the number read left to right for the warp (top red row) and from top down for the weft (left green column). Initially there are four bundles of four red strings in the warp representing the four bits of each of the master base line's four numbers. In the warp bundles, some strings are initially up (for bits that are 0) and some are initially down (for bits that are 1). In the illustration, a space is used to separate the bundles and is not part of the weave. In the weft are added four bundles of four green strings representing the master base line in the other direction. Each string in the weft bundles must be added separately. When the weft string added represents a 0, the warp threads are as described above prior to its addition. When the weft string represents a 1, the warp strings are reversed before the weft string is added. Each added weft string is pushed to the top prior to warp reversal or the addition of the next string. The resulting weave will be a 4x4 square of 16 4x4 patterns of strings. The diagonal of each of these smaller 4x4 square patterns from left top to right bottom will be the binary equivalent of the number at that position of the magic square. The numbers in the diagonals correspond to those seen in the Bit Codes by Position Magic Square Construction section.