{'fraction_non_alphanumeric': 0.053044862994893194, 'fraction_numerical': 0.01614408873757619, 'mean_word_length': 3.6340966921119593, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
The number of"magic"squares and hypercubes

We define a magic square to be a square matrix whose entries are nonnegative integers and whose rows, columns, and main diagonals sum up to the same number. We prove structural results for the number of such squares as a function of the size of the matrix and the line sum. We give examples for small sizes and show similar results for symmetric, pandiagonal magic squares, and magic hypercubes.

Magic squares have turned up throughout history, some in a mathematical context, others in philosophical or religious contexts. According to legend, the first magic square was discovered in China by an unknown mathematician sometime before the first century A.D. It was a magic square of order three thought to have appeared on the back of a turtle emerging from a river. Other magic squares surfaced at various places around the world in the centuries following their discovery. Some of the more interesting examples were recorded in Europe during the 1500s. Cornelius Agrippa wrote De Occulta Philosophia in 1510. In it he describes the spiritual powers of magic squares and produces some squares of orders from three up to nine. His work, although influential in the mathematical community, enjoyed only brief success, for the counter-reformation and the witch hunts of the Inquisition began soon thereafter: Agrippa himself was accused of being allied with the devil. Although this story seems outlandish now, we cannot ignore the strange mystical ties magic squares seem to have with the world and nature surrounding us, above and beyond their mathematical significance.
Despite the fact that magic squares have been studied for a long time, they are still the subject of research projects. These include both mathematical-historical research, such as the discovery of unpublished magic squares of Benjamin Franklin [12], and pure mathematical research, much of which is connected with the algebraic and combinatorial geometry of polyhedra (see, for example, [1], [4], and [11]). Aside from mathematical research, magic squares naturally continue to be an excellent source of topics for "popular" mathematics books (see, for example, [3] or [13]). In this paper we explore counting functions that are associated with magic squares.
We define a semi-magic square to be a square matrix whose entries are nonnegative integers and whose rows and columns (called lines in this setting) sum to the same number. A magic square is a semi-magic square whose main diagonals also add up to the line sum. A symmetric magic square is a magic square that is a symmetric matrix. A pandiagonal magic square is a semi-magic square whose diagonals parallel to the main diagonal from the upper left to the lower right, wrapped around (i.e., continued to a duplicate square placed to the left or right of the given one), add up to the line sum. Figure 1 illustrates our various definitions. We caution the reader about clashing definitions in the literature. For example, some people would reserve the term "magic square" for what we will call a traditional magic square, a magic square of order n whose entries are the integers 1, 2, . . . , n 2 .
Our goal is to count these various types of squares. In the traditional case, this is in some sense not very interesting: for each order there is a fixed number of traditional magic squares. For example, there are 880 traditional 4 × 4 magic squares. The situation becomes more interesting if we drop the condition of traditionality and study the number of magic squares as a function of the line sum. We denote the total number of semi-magic, magic, symmetric magic, and pandiagonal magic squares of order n and line sum t by H n (t), M n (t), S n (t), and P n (t), respectively.
We illustrate these notions for the case n = 2, which is not very complicated: here a semi-magic square is determined once we know one entry (denoted by * in Figure 2); a magic square has to have identical entries in each coefficient. Hence * t − * t − * * t/2 t/2 t/2 t/2 These easy results already hint at something: the counting function H n is of a different character than the functions M n , S n , and P n . The oldest nontrivial results on this subject were first published in 1915: Macmahon [9] proved that otherwise.
The first structural result for general n was proved in 1973 by Ehrhart [7] and Stanley [15].
Theorem 1 (Ehrhart, Stanley) . The function H n (t) is a polynomial in t of degree (n − 1) 2 that satisfies the identities The fact that H n is a polynomial of degree (n − 1) 2 was conjectured earlier by Anand, Dumir, and Gupta [2]. An elementary proof of Theorem 1 can be found in [14]. Stanley also proved analogous results for the counting functions for symmetric semi-magic squares. In this paper, we establish analogues of these theorems for the other counting functions mentioned earlier.
A quasi-polynomial Q of degree d is an expression of the form where c 0 , c 1 , . . . , c d are periodic functions of t and c d ≡ 0. The least common multiple of the periods of the c j is called the period of Q. With this definition we can state the first of our two main theorems.

Theorem 2
The functions M n (t), S n (t), and P n (t) are quasi-polynomials in t of degrees n 2 −2n−1, n 2 /2 − n/2 − 2, and n 2 − 3n + 2, respectively, that satisfy the identities We can extend the counting function H n for semi-magic squares in the following way. Define a semi-magic hypercube (also called a quasi-magic hypercube) to be a d-dimensional n × · · · × n array of n d nonnegative integers that sum to the same number t parallel to any axis; that is, if we denote the entries of the array by m j Again we count all such cubes in terms of d, n, and t; we denote the corresponding enumerating function by H d n (t). So H 2 n = H n , whose properties are stated in Theorem 1. Except for the case d = n = 3, which seems to have appeared first in [5], we could not find any other references. We prove the following result for general d.
We prove Theorem 2 in Sections 2 and 3. To be able to compute the counting functions M n , S n , and P n for specific n, we need the periods of these quasi-polynomials. We describe in Section 4 methods for finding these periods and hence for the actual computation of M n , S n , and P n . Finally, we prove Theorem 3 in Section 5. All of our methods are based on the idea that one can interpret the various counting functions as enumerating integer points ("lattice points") in certain polytopes.

SOME GEOMETRIC COMBINATORICS.
A convex polytope P in R d is the convex hull of finitely many points in R d . Alternatively (and this correspondence is nontrivial [16]), one can define P as the bounded intersection of affine halfspaces. If there is a hyperplane H = x ∈ R d : a · x = b such that P ∩ H consists of a single point, then this point is a vertex of P. A polytope is rational if all of its vertices have rational coordinates.
Suppose that P is a convex rational polytope in R d . For a positive integer t, let L P (t) denote the number of lattice points in the dilated polytope tP = {tx : x ∈ P} (see Figure 3). Similarly, define L * P (t) to be the number of lattice points in the (relative) interior of tP. Ehrhart, who initiated the study of the lattice point count in dilated polytopes, proved the following [6].
Theorem 4 (Ehrhart) . If P is a convex rational polytope, then the functions L P (t) and L * P (t) are quasi-polynomials in t whose degree is the dimension of P and whose period divides the least common multiple of the denominators of the vertices of P.
In particular, if P has integer vertices, then L P and L * P are polynomials. Ehrhart conjectured and partially proved the following reciprocity law, which was proved by Macdonald [8] (for the case that P has integer vertices) and McMullen [10] (for the case that P has arbitrary rational vertices). Theorems 4 and 5 mark the beginning of our journey towards a proof of Theorem 2. The connection to our counting functions M n , S n , and P n is the following: all the various magic square conditions are linear inequalities (the entries are nonnegative) and linear equalities (the entries in each line sum up to t) in the n 2 variables forming the entries of the square. In other words, what we are computing is the number of nonnegative integer solutions to the linear system where x is a column vector in R n 2 whose components are the entries of our square, A denotes the matrix determining the magic-sum conditions, and b is the column vector each of whose entries is t. We will use the convention that the entries of x are ordered by rows of the original square, and from left to right in each row, that is, the entries x jk of the square are being arranged as x = (x 11 , x 12 , . . . , x 1n , x 21 , x 22 , . . . , x 2n , . . . , x n1 , x n2 , . . . , x nn ) .
The matrix A is constructed accordingly. For example, if we study magic 3 × 3 squares, The first row of Ax = b represents the first row sum in the square: x 11 + x 12 + x 13 = t; the second and third row (x 21 + x 22 + x 23 = t and x 31 + x 32 + x 33 = t) are the sums of the second and third row in the square. The fourth row of Ax = b represents the first column sum in the square: x 11 + x 21 + x 31 = t; and the next two rows take care of the second and third column in our square. Finally the last two rows of Ax = b represent the two diagonal constraints x 11 + x 22 + x 33 = t and Furthermore, by writing b = t 1, where 1 signifies a column vector each of whose entries is 1, we can see that our counting function enumerates nonnegative integer solutions to the linear system A x = t 1, that is, it counts the lattice points in the t-dilate of the polytope provided that we choose the matrix A according to the magic-sum conditions. Note that P is an intersection of half-spaces and hyperplanes, and is therefore convex. No matter which counting function the matrix A corresponds to, the entries of A are 0s and 1s. We obtain the vertices of P by converting some of the inequalities x k ≥ 0 to equalities. It is easy to conclude from this that the vertices of P are rational. Hence by Theorem 4, M n , S n , and P n are quasi-polynomials whose degrees are the dimensions of the corresponding polytopes. Geometrically, the "magic-sum variable" t is the dilation factor of these polytopes.
The Ehrhart-Macdonald reciprocity law (Theorem 5) connects the lattice-point count in P to that of the interior of P. In our case, this interior (again, using any matrix A suitable for one of the counting functions) is described by The lattice-point count is the same as before, with the difference that we now allow only positive integers as solutions to the linear system. This motivates us to define by M * n (t), S * n (t), and P * n (t) the counting functions for magic squares, symmetric magic squares, and pandiagonal magic squares as before, but with the restriction that the entries be positive integers. Theorem 5 then asserts, for example, that M n (−t) = (−1) deg(Mn) M * n (t) .
On the other hand, by its very definition M * n (t) = 0 for t = 1, 2, . . . , n − 1. Hence we obtain M n (−t) = 0 for such t. Also, since each row of the matrix A defined by some magic-sum conditions has exactly n 1s and all other entries 0, it is not hard to conclude that M * n (t) = M n (t − n). Combining this with the reciprocity law (2), we obtain There are analogous statements for S n and P n . Once we verify the degree formulas of Theorem 2, (1) follows from the observation that

PROOF OF THE DEGREE FORMULAS.
Let's start with magic squares: by Theorem 4, the degree of M n is n 2 (the number of places we have to fill) minus the number of linearly independent constraints. In other words, we have to find the rank of the n 2 × (2n + 2) matrix Here we only show the entries that are 1; every other entry is 0. Similarly as in our example, the first n rows of A represent the n column constraints of our square (x 11 + · · · + x n1 = t, . . . , x 1n + · · · + x nn = t), the next n rows represent the row contraints of the square, and the last two rows take care of the diagonal constraints. The sum of the first n rows of A is the same as the sum of the next n rows, so one of the first 2n rows is redundant and we can eliminate it; we choose the (n + 1)th. Furthermore, we can add the difference of the first and (n + 2)th row to the (2n + 1)th and subtract the nth row from the (2n + 2)th. These operations yield the n 2 × (2n + 1) matrix These represent 2n + 1 linearly independent restrictions on our magic square. The polytope corresponding to M n therefore has dimension n 2 − 2n − 1: it lies in an affine subspace of that dimension, and the point (1/n, . . . , 1/n) is an interior point of the polytope. Hence, by Theorem 4, the degree of M n is n 2 − 2n − 1.
The case of symmetric magic squares is simpler: here we have n k=1 k = n(n + 1)/2 places to fill, and the row-sum condition is equivalent to the column-sum condition. Hence there are n + 2 constraints, which are easily identified as linearly independent. The dimension of the corresponding polytope, and the degree of S n , is therefore n 2 /2 − n/2 − 2.
Finally, we discuss pandiagonal magic squares. Again we have n 2 places to fill; this time the constraints are represented by the n 2 × 3n matrix Here the first n rows represent the column constraints in our square, the next n rows the pandiagonal constraints, and the last n rows the row constraints.
As in the first case, the sum of the first n rows equals the sum of the next n rows, as well as the sum of the last n rows. We can therefore eliminate the (2n)th and (2n + 1)th rows to get the n 2 × (3n − 2) matrix Finally, we can replace the (n + 2)th row by the sum of the (n + 1)th and (n + 2)th rows, the (n + 3)th row by the sum of the (n + 2)th and (n + 3)th rows, etc. Subtracting the (2n)th row from the (n + 1)th row gives a matrix of full rank, that is, rank 3n − 2. The dimension of the corresponding polytope, which is the degree of P n , is thus n 2 − 3n + 2. This finishes the proof of Theorem 2.

COMPUTATIONS.
To interpolate a quasi-polynomial of degree d and period p, we need to compute p(d + 1) values. The periods of M n , S n , and P n are not as simple to derive as their degrees. What we can do, however, is to compute for fixed n the vertices of the respective polytopes, whose denominators give the periods of the quasi-polynomials (Theorem 4). This is easy for very small n but gets complicated very quickly. For example, we can practically find by hand that the vertices of the polytope corresponding to S 3 are (2/3, 0, 1/3, 1/3, 2/3, 0) and (0, 2/3, 1/3, 1/3, 0, 2/3), whereas the polytope corresponding to S 5 has seventy-four vertices. To make computational matters worse, the least common multiple of the denominators of these vertices is 60; one would have to do a lot of interpolation to obtain S 5 . The reciprocity laws in Theorem 2 essentially halve the number of computations for each quasipolynomial. Nevertheless, the task of computing our quasi-polynomials becomes impractical without further tricks or large computing power. The following table contains data about the vertices of the polytopes corresponding to M n , S n , and P n for n = 3 and 4. It was produced using Maple.
Polytope corresponding to Number of vertices l.c.m. of denominators With this information, it is now easy (that is, easy with the aid of a computer) to interpolate each quasi-polynomial. Here are the results: if t is odd.

SEMI-MAGIC HYPERCUBES.
We now prove Theorem 3. The fact that H d n is a quasi-polynomial, the reciprocity law for this function and H d * n , and the location of special zeros for H d n follow in exactly the same way as the respective statements in Theorem 2. As in that case, the remaining task is to find the degree of H d n , that is, the dimension of the corresponding polytope. Again we have only to find the dimension of the affine subspace of R n d in which this polytope lives. We do so by counting linearly independent constraint equations.
To this end, let us write the coordinates of a point in the polytope corresponding to H d n as c(a 1 , . . . , a d ) with 1 ≤ a j ≤ n. These are real numbers that satisfy the constraints c(a 1 , . . . , a d ) ≥ 0 , n j=1 c(a 1 , . . . , a k−1 , j, a k+1 , . . . , a d ) = 1 (1 ≤ k ≤ d) .
Once the (n−1) d coordinates c(a 1 , . . . , a d ) with 1 ≤ a j ≤ n−1 are chosen, the remaining coordinates are clearly determined by the foregoing conditions. Therefore, there are at least n d −(n−1) d linearly independent constraint equations.
On the other hand, every constraint involves at least one of the n d − (n − 1) d coordinates c(a 1 , . . . , a d ) for which at least one a j equals n. Consider, say, the coordinate c(a 1 , . . . , a k , n, . . . , n) in which 0 ≤ k < d and a 1 , . . . , a k < n. We compute: c(a 1 , . . . , a k , n, . . . , n) = 1 − We could have changed the order of summation, that is, used the d − k constraints that are in force here in a different order. However, we would always end up with the same expression. This implies that all constraints involving c(a 1 , . . . , a k , n, . . . , n) with 0 ≤ k < d and a 1 , . . . , a k < n, but not involving any coordinate with a j = 1 for some j with j ≤ k, are equivalent to the one constraint c(a 1 , . . . , a k , n, . . . , n) = 1 − (1 − n) d−k n + (−1) d−k 1≤j k+1 ,...,j d ≤n−1 c(a 1 , . . . , a k , j k+1 , . . . , j d ) .
Therefore there are at most n d − (n − 1) d linearly independent constraint equations. Accordingly, the dimension of the polytope and the degree of H d n are (n − 1) d .

CLOSING REMARKS.
One big open problem is to determine the periods of our counting functions. The evidence gained from our data seems to suggest that the periods increase in some fashion with n. We believe the following is true: The functions M n , S n , and P n are not polynomials when n ≥ 5.
Results in [1] lend further credence to the validity of this conjecture. As for semi-magic hypercubes, Bóna showed in [5] that H 3 3 really is a quasi-polynomial, not a polynomial. We challenge the reader to prove: Conjecture 2 . The function H d n is not a polynomial when n, d ≥ 3.