# Magic square

In mathematics, a magic square of order n is an arrangement of n² numbers in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A normal magic square contains the integers from 1 to n².

Magic squares exist for all orders n ≥ 1 except n = 2, although the case n = 1 is trivial—it consists of a single cell containing the number 1. The smallest nontrivial case, shown below, is of order 3.

File:MagicSquare-ExplicitSums.png

The constant sum in every row, column and diagonal is called the magic constant, M. The magic constant of a normal magic square depends only on n and has the value

$\displaystyle M(n) = \frac{n(n^2+1)}{2}$

For normal magic squares of order n = 3, 4, 5, …, the magic constants (sequence A006003 in OEIS) are:

15, 34, 65, 111, 175, 260, …

## Brief history of magic squares

### The Lo Shu Square (3x3 magic square)

Chinese literature dating from as early as 2800 BC tells the legend of Lo Shu or "scroll of the river Lo". In ancient China, there was a huge flood. The people tried to offer some sacrifice to the river god of one of the flooding rivers, the Lo river, to calm his anger. Then, there emerged from the water a turtle with a curious figure/pattern on its shell; there were circular dots of numbers that were arranged in a three by three nine-grid pattern such that the sum of the numbers in each row, column and diagonal was the same: 15. This number is also equal to the 15 days in each of the 24 cycles of the Chinese solar year. This pattern, in a certain way, was used by the people in controlling the river.

The Lo Shu Square, as the magic square on the turtle shell is called, is the unique normal magic square of order three.

### The early squares of order four (4x4 magic squares)

The earliest magic square of order four was found in an inscription in Khajuraho, India, dating from the eleventh or twelfth century; it is also a panmagic square where, in addition to the rows, columns and main diagonals, the broken diagonals also have the same sum.

Yang Hui was one of the first mathematicians to study magic squares, or vertical and horizontal diagrams as they were called. He created several magic squares, including 4th order ones. 

### Cultural significance of magic squares

Magic Squares have fascinated humanity throughout the ages, and have been around for over 4,000 years. They were frequently found in a number of cultures, including Egypt and India, engraved on stone or metal and worn as talismans, the belief being that magic squares had astrological and divinatory qualities, their usage ensuring longevity and prevention of diseases.

The Kubera-Kolam is a floor painting used in India which is in the form of a magic square of order three. It is essentially the same as the Lo Shu Square, but with 19 added to each number, giving a magic constant of 72.

The magic square figures in Greek writings dating from about 1300 BC were used by Arab astrologers in the ninth century when drawing up horoscopes.

### Albrecht Dürer's magic square

The order-4 magic square in Albrecht Dürer's engraving Melancholia I is believed to be the first seen in European art. It is very similar to Yang Hui's square, which was created in China about 250 years before Dürer's times. The sum 34 can be found in the rows, columns, diagonals, each of the quadrants, the center four squares, the corner squares, the four outer numbers clockwise from the corners (3+8+14+9) and likewise the four counter-clockwise (the locations of four queens in the two solutions of the 4 queens puzzle ), the two sets of four symmetrical numbers (2+8+9+15 and 3+5+12+14) and the sum of the middle two entries of the two outer columns and rows (e.g. 5+9+8+12), as well as several kite-shaped quartets, e.g. 3+5+11+15; the two numbers in the middle of the bottom row give the date of the engraving: 1514.

### The Sagrada Família magic square

File:Ms sf 2.jpg
A magic square in architecture

The Passion façade of the Sagrada Família church in Barcelona, designed by sculptor Josep Subirachs, features a 4×4 magic square:

The magic sum of the square is 33, the age of Jesus at the time of the Passion. Structurally, it is very similar to the Melancholia magic square, but it has had the numbers in four of the cells reduced by 1.

## Types of magic squares and their construction

There are many ways to construct magic squares, but the standard (and most simple) way is to follow certain configurations / formulas which generate regular patterns. Magic squares exist for all values of n, with only one exception - it is impossible to construct a magic square of order 2. Magic squares can be classified into three types: odd, doubly even (n divisible by four) and singly even (n even, but not divisible by four). Odd and doubly even magic squares are easy to generate; the construction of singly even magic squares is more difficult but several methods exist, including the LUX method for magic squares (due to John Conway) and the Strachey method for magic squares. Only odd and doubly even magic squares are discussed below.

### A method for constructing a magic square of odd order

Starting from the central column of the last row with the number 1, the fundamental movement for filling the squares is diagonally down and right, one step at a time. If a filled square is encountered, one moves vertically up one square, then continuing as before. When a move would leave the square, it is wrapped around to the first row or last column, respectively.

The same pattern can be achieved starting from the central column of the first row; In this case the fundamental movement is diagonally up and left, one step at a time, and if a filled square is encountered, one moves vertically down one square, then continuing as before. When a move would leave the square, it is wrapped around the last row or first column, respectively.

Similar patterns can also be obtained by starting from other squares.

 File:MagicSquare-Order3.png File:MagicSquare-Order5.png File:MagicSquare-Order9.png Order 3 Order 5 Order 9

### A method of constructing a magic square of doubly even order

All the numbers are written in order from right to left across each row in turn, starting from the top right hand corner. Numbers are then either retained in the same place or interchanged with their diametrically opposite numbers in a certain regular pattern. In the magic square of order four, the numbers in the four central squares and one square at each corner are retained in the same place and the others are interchanged with their diametrically opposite numbers. In the magic square of order eight, the same is done; the 16 central squares and 4 squares at each corner are retained in their places and the rest are switched.

A general rule: If n represents the order of the doubly even square, retain numbers in the following pattern. The central square with sides of length n/2 should be retained. Also retain the squares with sides of length n/4 in each of the four corners.

 File:MagicSquare-Order8.png Order 8

## Counting magic squares

Counting the number of distinct normal magic squares is a difficult problem in combinatorics. Squares that differ trivially by a rotation or a reflection are considered equivalent and are counted just as one, see Frénicle standard form. Even so, the count increases rapidly with n. In fact, non-trivial exact results are known only for n = 4 and n = 5. The magic square count is sequence A006052 in OEIS.

Order Count Details
3 1 The Lo Shu Square
4 880 Frénicle de Bessy (1693) 
5 275305224 Richard Schroeppel (1973)
6 (1.7754 ± 0.0016) × 1019 Klaus Pinn and C. Wieczerkowski (1998),
through Monte Carlo simulation.
7 (3.7982 ± 0.0004) × 1034

## Generalizations

### Extra constraints

Certain extra restrictions can be imposed on magical squares. If not only the main diagonals but also the broken diagonals sum to the magic constant, the result is a panmagic square. If raising each number to certain powers yields another magic square, the result is a bimagic, a trimagic, or, in general, a multimagic square.

### Different constraints

Sometimes the rules for magic squares are relaxed, so that only the rows and columns but not necessarily the diagonals sum to the magic constant. In heterosquares and antimagic squares, the 2n + 2 sums must all be different.

### Other operations

Instead of adding the numbers in each row, column and diagonal, one can apply some other operation. For example, a multiplicative magic square has a constant product of numbers.

 File:MultiplicativeMagicSquare-Order3.png File:MultiplicativeMagicSquare-Order4.png M = 216 M = 6720

### Other magic shapes

Other shapes than squares can be considered, resulting, for example, in magic stars and magic hexagons. Going up in dimension results in magic cubes, magic tesseracts and other magic hypercubes.

### Combined extensions

One can combine two or more of the above extensions, resulting in such objects as multiplicative multimagic hypercubes. Little seems to be known about this subject.

## Related problems

### Magic Square of Primes

Rudolf Ondrejka discovered the following 3x3 magic square of primes, in this case nine Chen primes:

$\displaystyle \begin{bmatrix} 17 & 89 & 71 \\ 113 & 59 & 5 \\ 47 & 29 & 101 \end{bmatrix}$

### n-Queens problem

In 1992, Demirörs, Rafraf, and Tanik published a method for converting some magic squares into N-queens solutions, and vice versa.