The guide
Every Line Adds to the Same Number, but Odd, Doubly Even and Singly Even Squares Need Different Tricks
The three by three square of the Luoshu legend has one answer. Larger squares have millions, and building even one takes a method that depends on the size.
- n(n^2 + 1)/2 the sum of every line of a normal square of order n
- 362,880 arrangements of 1 to 9; exactly 8 of them are magic
- 880 order-4 squares, not counting turns and flips
- 275,305,224 order-5 squares, counted in 1973
Read the full guide Fold the guide
Write the numbers 1 to 9 in a three by three grid so that every row, every column and both diagonals add up to the same total. Try it for a minute. There is, in the end, essentially one way to do it.
Chinese tradition calls that arrangement the Luoshu, or Lo Shu, and also the Nine Halls Diagram, named for the Luo River near Luoyang. A legend about the flood-taming ruler Yu tells of a turtle rising from the river with circles of dots for the numbers one to nine on its shell. The legend is a legend, and the written record is more cautious. The Wikipedia account of the Luoshu says early records from 650 BCE are ambiguous and speak only of a river map, that clear references to a magic square appear by 80 CE, and that an explicit example is given from 570 CE.
The broader Wikipedia history of magic squares adds that the first unequivocal instance sits in a chapter of the first-century Da Dai Liji, a book of rites, and that the square was only identified with the legendary Luoshu chart in the twelfth century. By 1275 Yang Hui’s treatise showed squares far beyond order three. His MacTutor biography notes that he called them number diagrams, gave examples from order 3 up to order 10, and presented them as a good way to interest people in numbers, without claiming any magic for them.
The idea travelled. The same history dates the first datable order-4 square to 550 CE in India and describes an encyclopedia compiled in Baghdad around 983 with squares of orders 3 to 9. It credits Narayana with listing the order-4 pandiagonal squares in 1356, and his MacTutor biography says the last chapter of his Ganita Kaumudi gives rules for odd, even and doubly even perfect squares. In 1514 Albrecht Durer engraved an order-4 square with the total 34 into Melencolia I.
Why every line must reach n(n^2 + 1)/2
The common total is not a free choice. Add up every number in a normal square of order n, meaning one that holds 1 to n^2 once each. That sum is n^2(n^2 + 1)/2. The n rows divide it between them equally, so each row carries one nth of the whole, which is n(n^2 + 1)/2. Columns and diagonals must match the rows.
For order 3 the formula gives 15, for order 4 it gives 34, for order 5 it gives 65, then 111, 175 and 260. The sequence is A006003 in the OEIS. W. W. Rouse Ball gave the same formula at the start of the magic squares chapter of his Mathematical Recreations and Essays.
The formula also explains the one size that fails. A normal square of order 2 would need every line to reach 5. Call the top cells a and b and the bottom cells c and d. The top row and the left column both reach 5, so a + b equals a + c, which forces b to equal c. A normal square uses each number once. No 2 by 2 magic square can exist.
Rouse Ball reports that Cornelius Agrippa read this impossibility as a sign of the imperfection of the four elements. The arithmetic needs no symbolism at all.
Three sizes, three methods
Mathematicians sort the orders into three families. Odd orders form the first, doubly even orders divisible by four the second, and singly even orders such as 6, 10 and 14 the third. The Wikipedia article on constructions explains that entirely different techniques are needed for each family. It calls odd and doubly even squares easy to generate and the singly even case more difficult, which matches the experience of anyone who has tried a 6 by 6 square by hand.
For odd orders the classic recipe is the Siamese method. Simon de la Loubere brought it to France in 1688 on his way home from an embassy to Siam and published it in 1693, saying that he had learnt it from a Frenchman named Vincent, who had it from Surat in India. Put 1 in the middle of the top row. Move up and to the right for each next number, wrapping off the top to the bottom row and off the right edge to the left column. When the target cell is already filled, drop one cell down instead.
Try it on order 5, where every line must reach 5 x 26 / 2 = 65. The 1 sits in row 1, column 3. The move to 2 leaves the top, so 2 lands in row 5, column 4. Then 3 goes to row 4, column 5, the 4 wraps to row 3, column 1, and the 5 lands in row 2, column 2. The next move would hit the 1, so 6 drops below the 5. Carry on to 25 and the top row reads 17, 24, 1, 8, 15, which adds to 65. The tool replays all 25 steps and names the rule behind each one.
Why does it work? Rouse Ball’s explanation writes every number as two digits in base n. The walk places each units digit and each leading digit exactly once in every row and every column, so rows and columns reach the total. Each diagonal either collects one of every digit or the same middle digit n times, and both give the same sum. He also warns that starting the walk in another cell gives a square that is magic in rows and columns but, in general, not on its diagonals. That is why the tool refuses other starting cells and shows the failing diagonals instead of quietly moving the start.
Doubly even squares need no walk at all. Write 1 to n^2 in reading order. In every 4 by 4 block, keep the numbers on that block’s two diagonals and replace each other number x by n^2 + 1 minus x. For order 4 the counting grid becomes 1, 15, 14, 4 over 12, 6, 7, 9 over 8, 10, 11, 5 over 13, 3, 2, 16, which is the first example in Rouse Ball’s chapter.
Singly even orders resist both tricks. Strachey’s method, which the Wikipedia article traces to Rouse Ball’s 1911 edition, fills the four quarters with odd Siamese squares, swaps a few columns between quarters and finishes with two single-cell swaps. John Conway’s LUX method lays out a smaller array of the letters L, U and X, walks it in Siamese order and fills each letter’s two by two block in a fixed pattern. Both reach every order from 6 upward. They rarely agree cell by cell: in the tool’s own checks the two squares differed in 97 to 100 percent of their cells for every singly even order from 6 to 58.
Symmetry and counting
A square can be turned four ways and mirrored, which gives eight versions of one arrangement. Swap every number x for n^2 + 1 minus x and you get its complement, which is magic too. The Wikipedia article on magic squares lists these among the changes that keep a square magic, along with multiplying every number by a constant or adding the same amount to each. That last fact is why the numbers need not start at 1. Any arithmetic progression works, and the Siamese method article shows 5, 10 and so on up to 45 forming a square whose lines all reach 75.
So how many order-3 squares are there? There are 362,880 ways to place 1 to 9 in nine cells. The tool checks every one of them in the browser and finds exactly 8 magic squares. All 8 turn out to be turns and flips of a single square, which is why every account calls the 3 by 3 square unique up to symmetry.
Beyond three the counts explode. Bernard Frenicle de Bessy listed 880 order-4 squares, counted without turns and flips, in memoirs published in 1693. His MacTutor biography adds that his papers do not prove the list complete, and that a proof had to wait for Friedrich Fitting in 1931. The OEIS entry gives 275,305,224 squares of order 5, computed by Richard Schroeppel in 1973. A 2015 study in PLOS ONE estimated the counts for larger orders statistically and found that the chance of a random arrangement being magic falls faster than exponentially as the order grows.
Some squares carry extra structure. In a pandiagonal square the broken diagonals, which wrap around the edges, also reach the constant. None exists at order 3 and the smallest are 4 by 4. The Wikipedia article states that none can be made from consecutive numbers at any order of the form 4k + 2. The tool builds pandiagonal squares only by the two constructions it implements, for orders such as 5, 7, 11 and 13 and for multiples of four, and says plainly when it has no method.
Magic squares as puzzles
Blank out some cells and a magic square becomes a puzzle: restore the missing numbers so that every line reaches the constant again. The Wikipedia article calls solving partially completed squares a popular pastime and compares the reasoning to Sudoku. The constraints here are sums rather than symbols, so a line with a single empty cell gives its number away at once.
A fair puzzle needs exactly one answer, and that is easy to claim and harder to check. The generator removes cells one at a time and runs a complete backtracking search after each removal, keeping the blank only if the search still finds exactly one way to finish. It labels a puzzle unique only when that search runs to the end. Above order 6 it offers no puzzles at all, because such a proof could not finish in reasonable time inside a web page.
What these methods cannot do is worth saying too. Each recipe makes one square, or a handful more through symmetry, complements and progressions, out of the hundreds of millions that exist at order 5 alone. None of them samples squares at random or counts them. None finds a pandiagonal square of order 9 or 15 here, where the source describes a different construction that the tool leaves out.
The Luoshu still holds the whole subject in nine cells. One total is fixed by arithmetic, one shape is forced by that total, and eight orientations share a single answer.
Sources
Every source consulted for this guide. Links checked October 17, 2026.
- Magic square Wikipedia
- Luoshu Square Wikipedia
- Siamese method Wikipedia
- Constructions of magic squares Wikipedia
- Strachey method for magic squares Wikipedia
- Conway's LUX method for magic squares Wikipedia
- Pandiagonal magic square Wikipedia
- A006003: a(n) = n*(n^2 + 1)/2 The On-Line Encyclopedia of Integer Sequences
- A006052: Number of magic squares of order n composed of the numbers from 1 to n^2, counted up to rotations and reflections The On-Line Encyclopedia of Integer Sequences
- Mathematical Recreations and Essays Project Gutenberg ebook 26839 (W. W. Rouse Ball; public domain in the USA)
- Yang Hui (1238 - 1298) - Biography MacTutor History of Mathematics, University of St Andrews
- Frenicle de Bessy (1604 - 1674) - Biography MacTutor History of Mathematics, University of St Andrews
- Narayana (1325 - 1400) - Biography MacTutor History of Mathematics, University of St Andrews
- Numerous but Rare: An Exploration of Magic Squares OpenAlex catalogue record W1486296411 (Akimasa Kitajima and Macoto Kikuchi, PLOS ONE, 2015, CC BY)