NOTACAL logo

Magic Square Calculator

Magic Square Calculator

Give us your feedback! Was this useful?

Introduction

A magic square is a square grid of consecutive integers — usually starting at 1 — arranged so that every row, every column, and both main diagonals add up to the same total. That total is called the magic constant. The classic example is the 3×3 Lo Shu square, a puzzle that appears in Chinese legends dating back more than four thousand years. For a 3×3 square built from the numbers 1 through 9, every line sums to 15.

Magic squares are the oldest documented example of a recreational mathematical object, but they are far more than a curiosity. They sit at the intersection of combinatorics, group theory, and algorithm design. The construction methods used to build them — most notably the Siamese method developed by the French diplomat Simon de la Loubère in the late seventeenth century — are among the earliest recorded algorithms in mathematics, written down more than a century before the word "algorithm" carried its modern meaning.

This calculator implements the Siamese method to generate a magic square of any odd order from 3 to 15. You pick the size of the square, and the tool places every integer from 1 up to n² into the grid following a simple set of rules. Alongside the finished square, it reports the magic constant and verifies that every row, column, and diagonal really does sum to the same value — so you never have to trust the construction blindly.

Beyond the pure fun of watching a grid fill in, magic squares connect naturally to the Number Sequence Calculator (for exploring the arithmetic of the rows) and to the Matrix Calculator, since a magic square is simply a matrix with a special property. If you enjoy this kind of number puzzle, the math magic tricks guide collects several more tricks that rely on the same kind of elegant arithmetic.

How to Use This Calculator

Using the magic square generator takes a single choice: the order of the square you want.

Start by selecting the square order from the dropdown. The calculator supports odd orders of 3, 5, 7, 9, 11, 13, and 15. The order n determines both the size of the grid and the range of numbers used: an n×n square uses every whole number from 1 to n². A 3×3 square uses 1 through 9, a 5×5 square uses 1 through 25, and so on.

Once you select an order, the calculator immediately builds the square using the Siamese method and displays the grid. The results panel shows the magic constant, the square's dimensions, the total number of cells, and the range of numbers used. Below the grid, a verification message confirms whether every row, column, and both diagonals sum to the same total.

Example 1: The Lo Shu square (order 3)

Select order 3. The calculator produces the classic arrangement:

816
357
492

Here the magic constant is 15. Check the rows: 8 + 1 + 6 = 15, 3 + 5 + 7 = 15, and 4 + 9 + 2 = 15. The columns work too: 8 + 3 + 4 = 15, 1 + 5 + 9 = 15, and 6 + 7 + 2 = 15. Both diagonals follow: 8 + 5 + 2 = 15 and 6 + 5 + 4 = 15. Every line through the center sums to the same total.

Example 2: A larger square (order 5)

Select order 5. The calculator fills a 5×5 grid with the numbers 1 through 25, producing a magic constant of 65. You can verify any single row or column by hand — for instance, the top row of the standard Siamese construction reads 17, 24, 1, 8, 15, which sums to 65. With five rows, five columns, and two diagonals, that is twelve separate lines all meeting at the same total. The verification message confirms the property automatically.

Example 3: Maximum size (order 15)

Select order 15 to see the largest square this calculator builds. The grid contains 225 cells holding the numbers 1 through 225, and the magic constant jumps to 1,695. Generating a square this size by hand would take a dozen or more careful passes; the Siamese construction produces it instantly.

The Formula

The magic constant — the sum that every row, column, and diagonal must equal — depends only on the order n and the range of numbers used. Because the square contains every integer from 1 to n² exactly once, the total of all numbers in the grid is known, and dividing by the n rows gives the constant:

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

where n is the order of the square. For n = 3, this gives M = 3 × (9 + 1) / 2 = 15. For n = 5, M = 5 × (25 + 1) / 2 = 65. For n = 15, M = 15 × (225 + 1) / 2 = 1,695. [mathworld-magic]

The Siamese construction algorithm

The Siamese method — attributed to Simon de la Loubère, who learned of it in 1688 while serving as a French ambassador to Siam — builds an odd-order magic square with a few simple rules. [wikipedia-siamese]

  1. Place the number 1 in the middle cell of the top row.
  2. Move up one row and right one column.
  3. If that cell lies outside the grid, wrap around to the opposite edge.
  4. If that cell is already occupied — or if the move would land on the square that sits above-right of the top-right corner — place the next number directly below the current cell instead.
  5. Continue until every cell is filled.

The elegance of the method is that a single, repeatable rule fills the entire grid without any backtracking. Each number has exactly one possible home, so the algorithm runs in linear time relative to the number of cells. For a 3×3 square it places nine numbers in nine steps; for a 15×15 square it places 225 numbers in 225 steps. Cleve Moler, the creator of MATLAB, wrote a short program using this exact construction and observed that the resulting squares share a striking property: they are all symmetric under 180-degree rotation. [mathworks-moler]

Why it works

The Siamese construction works because it distributes the numbers so that each movement pattern — up, right, and diagonal — contributes equally across the rows and columns. In a square of odd order, the up-right diagonal movement cycles through all n² positions before repeating, visiting each row and column exactly n times. This even distribution is what guarantees every row, column, and diagonal accumulates the same sum. The Rose-Hulman Undergraduate Mathematics Journal published a rigorous proof of this property, showing how a Siamese-constructed square can be decomposed into two orthogonal Latin squares whose sums provably equal the magic constant. [rose-hulman]

Magic Constants Reference

The table below shows the magic constant for each supported order, along with the total number of cells and the largest number placed in the grid.

Order (n)Cells (n²)Numbers UsedMagic Constant M = n(n²+1)/2
391 – 915
5251 – 2565
7491 – 49175
9811 – 81369
111211 – 121671
131691 – 1691,105
152251 – 2251,695
The magic constant grows roughly cubically with the order of the square. Doubling the order from 7 to 15 multiplies the constant by nearly ten, because both the grid size and the number range expand together.

Notice how quickly the constant climbs. Moving from a 7×7 to a 9×9 square more than doubles the constant, from 175 to 369. This rapid growth reflects the n² term in the numerator: adding two cells to each side adds far more than two new numbers to sum.

Practical Tips

  • Start small to verify by hand. Before trusting the algorithm with a 15×15 grid, build a 3×3 square and check the rows, columns, and diagonals yourself. The Lo Shu square is small enough to verify completely in under a minute, and it builds confidence in the construction rule.
  • Use the constant to spot-check quickly. You never need to sum every line. Pick two rows and one column; if all three equal M and the diagonal matches too, the construction has almost certainly worked. With the Siamese method, the verification message in the calculator does this check automatically.
  • Check that your order is odd. The Siamese method only produces magic squares of odd order. If you attempt an even order, the up-right movement pattern breaks down and the diagonal sums drift away from the constant. This calculator restricts the dropdown to valid odd orders for exactly that reason.
  • Explore rotations and reflections. Rotating or reflecting a valid magic square produces another valid magic square. From a single Lo Shu square, you can generate eight distinct 3×3 squares by rotation and reflection — a nice exercise for the Matrix Calculator, which can transform the grid for you.
  • Pair with other number tools. The rows of a magic square form number sequences you can analyze with the Number Sequence Calculator, and the modular arithmetic behind the Siamese wrap-around is a natural fit for the Modulo Calculator.
  • Use squares for low-stakes games. A magic square of order 3 has exactly eight winning lines (three rows, three columns, two diagonals), which makes it a classic framework for number-based puzzles and party games. Just remember that the math is the point — the square is a curiosity, not a gambling aid.

Limitations

  • Odd orders only. This calculator implements the Siamese method, which constructs magic squares only for odd n. Even-order squares (4×4, 6×6, and so on) exist but require entirely different construction techniques, such as the strachey method or the LUX method for singly-even orders. Those are outside the scope of this tool.
  • Consecutive numbers from 1 only. The construction always uses the integers 1 through n². If you want a square built from a different starting value or a non-consecutive set, you cannot generate it directly here, though you could add a constant to every cell of the result — a transformation that preserves the magic property.
  • Fixed construction orientation. The Siamese method produces one canonical orientation of each square. Rotations and reflections yield valid alternatives, but this calculator does not expose a "rotate" or "reflect" control; you would need to perform those transforms yourself.
  • Not a proof tool. The calculator verifies the sums of the grid it builds, but it does not prove the general theorem that the Siamese method works for every odd order. For a rigorous treatment, see the Rose-Hulman journal article and the EMS Press survey cited below. [ems-press]
  • Educational framing. Magic squares are recreational mathematics. While the Lo Shu square has historical ties to feng shui and the magic constant to mystical numerology, the value here is algorithmic and mathematical, not supernatural.
  • References are cited inline. Every source used to build this calculator is listed in the references section at the bottom of this page, and each major claim carries a citation. For a deeper mathematical treatment of magic squares, the Rose-Hulman journal article and the EMS Press survey are excellent starting points; the MathWorks blog offers a modern programmer's view of the classic algorithms. [brandeis-pdf]

Frequently Asked Questions

What is a magic square?
A magic square is a square grid filled with consecutive integers, usually starting at 1, arranged so that every row, every column, and both main diagonals sum to the same value, called the magic constant.
What is the magic constant for a 3x3 square?
For a 3x3 square using the numbers 1 through 9, the magic constant is 15. Every row, column, and diagonal sums to 15.
How do I calculate the magic constant?
Use the formula M = n(n² + 1) / 2, where n is the order of the square. For n = 5, M = 5 × 26 / 2 = 65.
What is the Siamese method?
The Siamese method, credited to Simon de la Loubère, constructs an odd-order magic square by placing 1 in the middle of the top row, then moving up-right with wrap-around, and dropping down one cell whenever the target is occupied.
Does this calculator support even-order magic squares?
No. The Siamese method only works for odd orders, so this calculator supports 3, 5, 7, 9, 11, 13, and 15. Even-order squares require different construction algorithms not implemented here.
How many magic squares exist for a given order?
The count grows explosively with order. There is exactly one 3x3 magic square up to rotation and reflection (the Lo Shu), and it has 8 rotated/reflected forms. For order 4 there are 880 essentially different squares, and for order 5 the count exceeds 275 million.
What is the Lo Shu square?
The Lo Shu is the unique 3x3 magic square, recorded in Chinese tradition roughly four thousand years ago. Its rows, columns, and diagonals all sum to 15, and the number 5 occupies the center.
Are magic squares useful for anything practical?
They have found niche uses in experimental design, error-correcting codes, and even puzzle-based marketing, but their primary value is recreational and pedagogical — they are an elegant introduction to algorithms, modular arithmetic, and combinatorics.
What is a normal magic square?
A normal magic square uses the consecutive integers 1 through n², exactly once each. This calculator produces normal magic squares of odd order. Non-normal squares can use other ranges or repetitions.
Can I make a 6x6 magic square?
Yes, 6x6 magic squares exist, but they require the singly-even construction method (Strachey's method) rather than the Siamese method. This calculator does not implement that algorithm.

References

  1. [1]Wikipedia. (n.d.). Siamese method.
  2. [2]Moler, C. (2012). Magic squares, part 2: Algorithms. MathWorks MATLAB Blog.
  3. [3]John Carroll University. (n.d.). Magic squares vignette.
  4. [4]Weisstein, E. W. (n.d.). Magic square. MathWorld — A Wolfram Web Resource.
  5. [5]Arroyo, J. (2018). A proof of the "magicness" of the Siam construction of a magic square. Rose-Hulman Undergraduate Mathematics Journal, 19(1), Article 3.
  6. [6]Weisstein, E. W. (n.d.). Magic square. Wolfram MathWorld (PDF via Brandeis University).
  7. [7]European Mathematical Society. (n.d.). Magic squares: A historical and computational tour (EMS Press).

Last updated: July 31, 2026

1b

UnByte — Independent Software Engineering

Every calculator references authoritative sources — Editorial policy