0% found this document useful (0 votes)
16 views7 pages

AIMagic SQ

MAgic Squares

Uploaded by

Asier Arregi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

AIMagic SQ

MAgic Squares

Uploaded by

Asier Arregi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Applications of AI for Magic Squares

Jared Weed
Department of Mathematical Sciences
Worcester Polytechnic Institute
Worcester, Massachusetts 01609-2280
Email: [email protected]

Abstract—In recreational mathematics, a normal magic square


is an n×n square matrix whose entries are distinctly the integers
arXiv:1602.01401v1 [math.HO] 3 Feb 2016

1 . . . n2 , such that each row, column, and major and minor


traces sum to one constant µ. It has been proven that there are
7,040 fourth order normal magic squares and 2,202,441,792 fifth
order normal magic squares [4], with higher orders unconfirmed
[3]. Previous work related to fourth order normal squares
has shown that symmetries such as the dihedral group exist
[5] and that (under certain conditions) normal magic squares
can be categorized into four distinct subsets. [2][6] With the
implementation of an efficient backtracking algorithm along with
supervised machine learning techniques for classification, it will
be shown that the entire set of fourth order normal magic
squares can be generated by expanding the symmetry groups
of 95 asymmetric parents. Discussion will suggest that methods
employed in this project could similarly apply to higher orders.

I. I NTRODUCTION

Magic squares, being a recreation mathematical topic in


nature, have been the subject of entertainment and interest to
many mathematicians and math-enthusiasts alike for hundreds
of years. A normal magic square is an n × n square matrix
whose entries are distinctly the integers 1 . . . n2 , such that each
row, column, and major and minor traces sum to one constant
µ. Magic squares of order 3 and 4 (and even higher orders)
have appeared in paintings, literature, and artifacts dated as
far back as 650 BC. One of the first magic squares found in
ancient Chinese literature is the Lo Shu square, which is told
to have been painted on the shell of a sea turtle: the center 2 by 2 square, the four outer corner squares,
and the four outer corner squares of each 3 by 3 subsquare.
4 9 2 Furthermore, the sum can be found clockwise from the corners
3 5 7 (3 + 8 + 14 + 5) and counter-clockwise (5 + 15 + 12 + 2).
8 1 6 What’s most fascinating about the Durer square is the bottom
row: 1514 was the year the painting was made, and 4 and
One of the most notable magic squares in western civiliza- 1 correspond to the letters D and A, the initials of Albrecht
tion is the order 4 magic square in Albrecht Durer’s engraving Durer.
Melencolia I: Today, we now know that there are exactly 7,040 fourth
16 3 2 13 order normal magic squares and 2,202,441,792 fifth order
5 10 11 8 normal magic squares in existence. What’s most intriguing is
9 6 7 12 that, like most other famous mathematicians of the time in our
4 15 14 1 past, results were found without the modern-day computing
and research power we have access to today. Algorithms were
The Durer magic square has many fascinating properties. developed to create magic squares of any size, without ever
Aside the rows and columns (and major/minor knowing how many there really are. This paper seeks to unveil
diagonal) summing to the magic number 34, the that question: How to develop an algorithm that creates magic
sum can also be found in each 2 by 2 quadrant, squares while systemically showing the methods it uses.
II. R ELATED W ORK Furthermore, since the magic squares in these groups shared
For any normal magic square of order n, by careful algebra determinants, Trigg showed that certain row and column
one can show that operations could be performed on magic squares preserving
the determinant, but reordering the configuration of half-
n(n2 + 1)
µ= sums within that group. For instance, by interchanging the
2 second and third rows, then interchanging the second and third
which, for the case of n = 4 is the number 34. In a 1917 columns of a Type II magic square, the result would be a Type
publication of Amusements in Mathematics, Henry Dudeney, I magic square (both within the Type A group). Alternatively,
an English mathematician specializing in logic puzzles and interchanging the third and fourth rows, then interchanging the
mathematical games, used this important fact and found a way third and fourth rows of a Type III magic square results in a
to classify all 7040 fourth order normal magic squares into 12 Type I magic square as well.
distinct configurations (coincidentally named Types I - XII).
His classification was developed by considering the fact that
the numbers from 1..16 contain 8 pairs of numbers that sum
to 17 = µ2 , and diagrams can be drawn for each magic square
to show these pairings (Fig 1.)

Fig. 2. Trigg Classification and Staab Transformations

Further research by Peter Staab in 2010 investigated the


Trigg groups to find if additional operations existed within
the groups to reconfigure one Dudeney type magic square to
Fig. 1. The 12 Dudeney Classification Types another, and if operations existed which did not affect the
configuration of the square. Interestingly, he noted that Type
These 12 classifications are independent of orientation, as VI needed to be split within the Trigg Type B into two distinct
many of the diagrams are not symmetric about some axis subsets: Type VI’ and Type VI”. This was due to some of
of reflection or rotation. Given this classification scheme, the Type VI magic squares having the additional property
Dudeney later showed precisely how many of the 7040 magic that a broken diagonal also summed to µ = 34. After this
squares were of each type reclassification, he found the following transformation groups
existed for the Trigg classification (Fig 2.):
Type I Type II Type III Type IV Type V Type VI
384 384 384 768 768 2432 A1 indentities and even bisymmetric transformations
A2 semisymmetric transformations
Type VII Type VIII Type IX Type X Type XI Type XII
448 448 448 448 64 64
A3 broken-diagonal (translation) transformations
A4 odd bisymmetric and rotational transformations
Curiously, one may note that Type I-III have an equivalent C1 identity transformations
number of magic squares, as do Type VII-X and Type XI- C2 odd bisymmetric transformations
XII. Looking back to Fig. 1, it also seems that these groups C3 even bisymmetric transformations
have similar structures in the configurations of half-sums. By C4 rotational transformations
1948, in a paper Determinants of Fourth Order Magic Squares Lastly, although tangential to the purpose of this paper, it is
published in the American Mathematical Monthly, C.W. Trigg worth mentioning one key result from 1998. K. Pinn and C.
proved that these similarities between certain Dudeney types Wieczerkowski approximated that, through the use of Parallel
existed by calculating their determinants. Trigg categorized the Tempering Monte Carlo simulations, the total number of sixth
12 Dudeney types into 4 distinct groups (unexcitingly named order normal magic squares is (1.4196 ± 0.00128) · 1020 .
Type A-D). Indeed, Type A was the set of Type I-III, Type Implications from these findings suggest that modern day
B the set of Type IV-VI, Type C the set of Type VII-X, and computing power may still not be fast enough to work in the
Type D the set of Type XI-XII. space of sixth order magic squares.
III. P ROBLEM S TATEMENT of magic squares G is simply the set of magic squares that
Given that much work has been done in categorizing the are pairwise asymmetric and together generate the entire set
fourth order magic squares by their symmetries and pairwise G using the transformation group of G. With these definitions
similarities, the next logical step would be to investigate in place, we are ready to approach the question to be answered:
these symmetry groups to conclude which of the 7040 magic
squares are necessary to generate the fourth order space. We Of the fourth order normal magic squares, how many
make the following definitions: unique generators are necessary and sufficient to generate
the entire space, and what are the associated transformation
Def: An n × n permutation matrix is a square matrix groups for these generators?
whose entries are {0, 1} such that 1 is in each column and
each row of the matrix exactly once. As mentioned in the previous section, progress has been
made towards the answer; however, the lower bound for
A permutation matrix P acting on a matrix A of generators itself is unconfirmed and the categorization of
similar dimension essentially reorders either the rows or transformation groups is incomplete. Successfully determining
columns of that matrix dependent on whether it is a left- a lower bound and completing the transformation groups
or right-multiplication. For example, Consider the following would provide for two key results:
matrix multiplication: 1) That there exists a set of magic squares for the n-th order
     that describe the entire space and
1 0 0 1 2 3 1 2 3 2) That this space can be described by a categorical enu-
 0 0 1  4 5 6  =  7 8 9 
meration of transformation groups imposed on the space.
0 1 0 7 8 9 4 5 6
Since the topology of matrices is well-defined and inherit
Note that the second and third rows of this matrix are many nice algebraic properties, the above results would hold
swapped after being multiplied by the permutation matrix on for the space of magic squares of any order. This suggests
the left. Thus P A permutes rows and AP permutes columns, that such a finding would be a methodical approach towards
while P AP permutes both rows and columns. identifying the properties of higher order magic squares with
relative ease.
Def: The symmetry group of a set of magic squares G
is the set of all pairs of permutation matrices {Pi , Pj } in IV. M ETHODS
Mn×n such that for any magic square A in the set G,
Pi APj and Pi AT Pj is also a magic square and in the set G. a b c d
Often times this will be called the transformation group of G. e f g h
i j k l
Def: Two magic squares A and B are symmetric if m n o p
and only if B can be written as Pi APj or Pi AT Pj for a pair
Theory
of permutation matrices {Pi , Pj }.
As mentioned previously, a normal magic square A of the
As an example, in Fig. 2 the set G can be thought fourth order has the following properties:
of as Trigg’s type A, and the transformation group of that set • ai,j ∈ A =⇒ ai,j ∈ {1, . . . , 16}
is any pair of permutation matrices from the set A1 , A2 , A3 , • Each ai,j distinct
or A4 . Clearly any magic square in that set will be again be n(n2 +1)
• µ= 2 = 34
a magic square in the set if any of the former transformations • Each row, column, and trace sum to µ
are applied. The last two properties will be investigated first. Let’s consider
the generic 4 × 4 matrix from above that inherits these
Def: The order of a transformation group T of G (denoted properties. We may notice that this produces a total of 10
as T (G)) is the cardinality of the set, i.e., |T (G)|. linear constraints, namely
1) a + b + c + d = 34
Def: A magic square A of a set of magic squares G is
2) e + f + g + h = 34
a generator of G if
3) i + j + k + l = 34
1) The transformation group of G applied to A generates 4) m + n + o + p = 34
a subset of the elements of G and 5) a + e + i + m = 34
2) For any two generators Ai , Aj of G, the subsets of G 6) b + f + j + n = 34
generated by Ai and Aj are pairwise disjoint. 7) c + g + k + o = 34
8) d + h + l + p = 34
The choice of the word generator in this case is not used in 9) a + f + k + p = 34
the traditional sense for algebra. A set of generators of a group 10) d + g + j + m = 34.
However, the solution to the system of simultaneous linear
equations above finds that of the 16 variables in the matrix,
only 7 are independent while the remaining 9 are necessarily
defined by their dependence. That is to say, a fourth order
normal magic square can be uniquely defined by only 7 entries,
thus lowering the dimensionality of the problem greatly. For
example, assume the entries a, b, c, e, f, g, i are independent
(the choices for these being independent will be examined
later), then
Fig. 3. Backtracking Algorithm by placing numbers into matrix
d = 34 − a − b − c
h = 34 − e − f − g
j = 2a + b + c + e − g + i − 34 Should a valid assignment occur for a, b, c, then the number
k = 68 − 2a − b − c − e − f − i resulting from 34−a−b−c should be removed from the list of
l =f +g−i available terms for the backtracking algorithm to test. This is
m = 34 − a − e − i necessary, as without it, deeper iterations would test this num-
n = 68 − 2a − 2b − c − e − f + g − i ber as an assignment despite it being necessarily assigned prior
o = 2a + b + e + f − g + i − 34 to d. After computing the first row, the algorithm computes
p = a + b + c + e + i − 34. the first column, having already registered the assignment for
This does not necessarily suffice in showing that arbitrary a. The choice for this is not so trivial: The cells e, f are also
numbers chosen for the entries a, b, c, e, f, g, i will guarantee considered ”free” since they will not propagate any constraints.
a normal magic square. It is certainly true that the constraints Because of this, ”free” cells check for every valid assignment
above will hold so that each row, column, and diagonal sum from the list of available numbers. The goal would then be to
to 34, but often times (at least with an arbitrary arrangement), avoid ”free” cells until absolutely necessary. Assigning the first
entries may be duplicated, and others could be negative, column requires only one ”free” cell, which in turn requires
0, or larger than 16. However, if we condition the above much less complexity.
constraints using the first two properties from above, we yield
an important restriction on the arrangement of numbers for the
independent cells: They must be configured in such a way as
to obey the laws that every cell must contain a distinct number
from the set {1, . . . , 16}.
Implementation
Determining the arrangements which do not follow this
law requires an algorithmic approach, due to having no prior
knowledge of which arrangements yield a normal magic
square. Because we are tasked with many constraints, of which Fig. 4. Constraint propagation of values resulting in µ = 34
require very specific parameters (each entry being distinct),
a backtracking algorithm that used constraint propagation After completing the first column, the last and final step
seemed to be the most logical implementation, considering of the backtracking algorithm is to compute the assignment
the analysis was focused on creating an efficient, dynamic for f, g, as they are the last two remaining cells necessary
system. Furthermore, backtracking has the property of quickly to be assigned to thusly define the square unique to these 7
withdrawing from partial solutions which are guaranteed to numbers. From above, f is a free cell, so values are arbitrarily
not resolve as a full solution—for instance, when one of assigned from the available list. The cell g requires the most
the constraints won’t be met since the number necessary effort: Since this is the final cell, the number assigned here
to be assigned has already previously been assigned. These must be such that all constraints that contain g are satisfied,
assignments are carried out by the constraint propagation, and the dependence values are available. If either of these
which accounts for all properties listed in the previous section. two assumptions fail, then g cannot be assigned and the
The algorithm begins by placing arbitrary numbers along backtracking algorithm withdraws from this arrangement of
the top row into slots a, b, c. The numbers in slots a, b are numbers.
considered ”free” since they do not propagate any constraints, Once the algorithm successfully generates a normal magic
however, after a and b have been assigned, c must be assigned square using the independent 7 numbers for the cells (Fig. 5),
in such a way that: 34 − a + b + c is a valid number (1 to a simple supervised machine learning technique is employed
16) that has not been propagated or previously assigned. For to find the pairs of 17 (Dudeney’s classification scheme) in
instance, if a = 1, b = 3 then the assignment c = 15 would be the magic square that can uniquely classify it. This is done by
invalid, since 34 − a − b − c = 15 which is previously assigned creating the classification scheme prior to using the diagram
to c. in Fig. 1.
Fig. 5. Resultant magic square given by 7-dimension basis from above Fig. 8. Candidate magic square from symmetric transformation group

Finally, an arbitrary magic square was chosen from each


transformation group within the Trigg type, and had applied
to it every set of permutation matrices defined on that magic
square. As the permutations were applied, magic squares
that were symmetric were deleted from the group, until the
transformation group had been exhausted. If more than one
magic square remained in the group, the process was repeated
with another magic square from the group (which clearly
Fig. 6. Categorization by finding sums of µ/2 = 17 defined another subset of magic squares which it generates)
until the minimum was reached.
Then, using the 7 numbers that define the magic square, the
program naively iterates across the set to find the pairs. This
is surprisingly efficient, since it does not have to iterate across
all 16 numbers to find pairs, and the classification scheme
guarantees that only two pairs are necessary to determine the
Dudeney Type.

Fig. 9. Removal of magic squares in the group based on rotations and


transposition

An example of this transformation group search can be seen


for the third-order case in Fig. 8, 9, and 10. If we consider
the dihedral group (rotations, reflections, transpositions) as the
transformation group for this set, then you can see that as the
Fig. 7. Resultant classification type given by configurations of sums Candidate square rotates about, transposes, and reflects about
each axis, it becomes symmetric with every other magic square
After each square is generated and classified, the next step in the set, thus deleting them from the group. Note that since
was to classify each square, as an extension, by their trans- there are only 8 normal magic squares of order 3, that the
formation groups. The properties necessary for this included candidate magic square is indeed the sole generator of the
their innate symmetries (such as how many broken diagonals entire group.
also added to µ), which Trigg group they were in, and also
by using previous research of the permutation sets Staab had
proven.

Fig. 10. Resultant unique generator for order 3 magic squares


V. R ESULTS as Type B was a total of 46 generators, each belonging to
Even though there are 7040 magic squares, results found one of 4 different transformation groups. The transformation
that only 95 magic squares are necessary and sufficient to groups were found to have orders 192, 96, 64, and 32, with
generate the entire space of fourth order. What was immedi- generators 12, 4, 10, and 20 respectively. Unlike Type A, Type
ately perplexing about this number was how it was distributed B space could not be uniquely defined by its generators and a
among the different Trigg Types. For the Type B and Type transformation group; however, if one were to further reduce
C magic squares, the resultant number of generators for the Type B into 4 pairwise disjoint subsets (aptly named B-1, B-2,
group did not evenly divide the enumeration of the magic B-3, and B-4) containing only the generators of a particular
squares classified as that type; the expectation was that a trans- transformation group, then we can indeed uniquely define each
formation group would provide a reduction of the set by some subgroup by its generators and the transformation group.
factor, but evidence suggested that additional information was
necessary. Trigg Type C

Trigg Type A While analysis of the Trigg Type B group provided in-
triguing results, Trigg Type C—Dudeney Types VII-X—had
The magic squares that exist in this group, Dudeney Types I- perhaps the most surprising result of all four types. Given that
III, have the additional property that each broken diagonal also there were 1792 magic squares in Type C evenly distributed
sums to µ = 34. Because of this property, many symmetries among all four Dudeney types, the assumption would be that
are introduced into this group. Analysis of the results found the behavior of this group was much like Type A. In fact,
that, from the 1152 magic squares classified as Type A, only
3 generators are necessary to generate the entire group (Fig.
11).

Fig. 13. Generators for Type C

Fig. 11. Generators for Type A it was found that there were 44 generators for this group,
each belonging to one of 2 different transformation groups.
Thus each generator from this set has a transformation group The transformation groups were found to have orders 64 and
of order 384. Furthermore, we can claim that the entire Type 32, with generators 12 and 32, respectively (Fig. 13). Again,
A space is uniquely defined by these 3 generators and the because of this result, the justification that Type C could be
transformation group of order 384. One observation worth uniquely defined by a set of generators and a transformation
mentioning is how similar each of the generators are; Only group was invalid. By imitating the revision for Type B and
8 of 16 cells differ between each generator, and yet there is splitting the Type C group into two disjoint subsets (C-1, C-
no arrangement of permutations in the confines of the space 2) categorized by separate generators and their transformation
to transform one to the other. sets, we again arrive at a distinction of uniquely defining a
group of magic squares based on those generators.
Trigg Type B
Analysis of the Dudeney Types IV-VI provided the most Trigg Type D
interesting results, primarily in the fact that so little was
With the small amount of magic squares classified Dudeney
previously understood. It was mentioned earlier that Type VI
Type XI-XII in this group (128), the results were expectantly
had the additional property that one of its broken diagonals
underwhelming and categorically likened to Type A. The
also summed to µ = 34. The assumption, then, would be that
exception in this case is that Trigg Type D magic squares
this group would undoubtedly have generators belonging to
lack many symmetries which would provide for a high-ordered
two separate transformation groups.
transformation group (Fig. 1).

Fig. 12. Generators for Type B

This was found to be not entirely true (Fig. 12). In fact,


hidden inside the group of 3968 magic squares classified Fig. 14. Generators for Type D
It was found that only 2 generators were necessary to gener- VII. C ONCLUSION
ate the space of Type D magic squares, and each generator had The finding of 95 generators for the entire set of 7040
a transformation group of order 64 (Fig. 14). Much like Type fourth-order normal magic squares improves the previous
A, without the need to reclassify the group, we can uniquely lower bound of 220 found in literature[2]. Furthermore, while
define the Type D magic squares by these two generators and many of the transformation groups were correctly catego-
the corresponding transformation group of order 64. Another rized by Staab [5], results show that previously unknown
observation worth mentioning related to the similarity of these transformation groups exist in the space, and that the Trigg
generators: There’s practically none, and yet due to the lack of classification requires revisions to reflect the true underlying
symmetry within the group, no arrangement of permutations structure. With these two results combined, by virtue of the
in the confines of this space generate one from the other. methods used in this analysis, such a construction can be
VI. D ISCUSSION developed with ease for fifth order magic squares which has
previously been limited by inefficient methods. With this paper
The results above motivate a theoretical discussion on the
as reference, a curious individual in the field of recreational
potential of the methodology scaled to higher orders. Firstly,
mathematics may use these tools to yield many fascinating
it was found that very minimal information was necessary to
results about fifth and sixth order normal magic squares.
completely describe the entire space of fourth order magic
squares. From the previous sections, it was noted that the R EFERENCES
problem in itself could be lower-dimensionalized to a basis [1] H. E. Dudeney, Amusements in Mathematics, London: Thomas Nelson,
of only 7 out of the 16 numbers that define a magic square. 1917
Further, only 95 generators are sufficient to generate the 7040 [2] M. Gardner, Mathematical Games Scientific American iss. 234, pp. 118-
122, 1976
magic squares for order four. This implies, that given the [3] K. Pinn, C. Wieczerkowski, Number of Magic Squares From Parallel
knowledge of Trigg classification and transformation groups, Tempering Monte Carlo arXiv.org, 1998
less than 700 bytes of information (7 ∗ 95 and assuming each [4] R. Schroeppel, Appendix 5: The Order 5 Magic Squares pp. 1-16, 1973
[5] P. Staab, The Magic of Permutation Matrices: Categorizing, Counting,
number requires one byte of storage) is required. and Eigenspectra of Magic Squares arXiv.org, 2010
Since we know some properties about higher order magic [6] C. W. Trigg, Determinants of Fourth Order Magic Squares
squares, such as transformation groups of odd order have the American Mathematical Monthly iss. 55, pp. 558-561, 1948
same behavior as transformation groups of one less order
(even), we can hypothesize that this scheme is adaptable–
at least for the fifth order case. This is further emphasized
by the fact that the methods used in this analysis were
efficient and easy to implement. With careful linear algebra,
one can show that the fifth order magic squares can be lower-
dimensionalized to a basis of only 13 out of 25 numbers that
define that magic square. The implications from this suggest
that a backtracking algorithm with constraint propagation may
still be suitable and efficient enough with modern computing
power to produce meaningful results.
Lastly, there is consideration to be had to appropriate a
better approximation (or more concrete bound) on the total
number of sixth order magic squares. While a previously
mentioned result from 1998 showed an approximation for this
number, certain applications from this analysis could perhaps
be abstracted to find the theoretical transformation groups of
the set, and determine from those what the generators of
those sets would look like. While there is no information
currently in literature suggesting any formulation of ”Trigg
Types” for these magic squares, one could again theorize their
configurations based on small simulations of this method and
applying transformation groups to this small subset.

You might also like