Magic Square Calculator
Choose a square order from 3 to 9 and the calculator generates a complete magic square where every row, column, and both main diagonals sum to the same value, called the magic constant. You also get the total sum, the count of numbers used, and the construction method. The "Show your work" panel walks through the key formulas.
Formula
Worked example
For a 3x3 magic square: n=3, so the magic constant M = 3(9+1)/2 = 15. The 9 cells hold integers 1-9 summing to 45 = 3 x 15. The Lo Shu arrangement (2 7 6 / 9 5 1 / 4 3 8) is the unique solution up to rotation: every row, column, and diagonal sums to 15.
What is a magic square?
A magic square is a square grid filled with distinct positive integers arranged so that every row, every column, and both main diagonals each add up to the same number, called the magic constant or magic sum. The most common form uses the consecutive integers from 1 to n^2, where n is the side length (the order). The 3x3 square is the simplest non-trivial example, and its arrangement is essentially unique: up to rotation and reflection there is only one way to place the numbers 1 through 9 so every line sums to 15. Magic squares appear across thousands of years of mathematical history, from the Chinese Lo Shu dating to around 650 BC, through Islamic and Renaissance mathematicians, to modern combinatorics and recreational math.
The magic constant formula
For a normal magic square of order n (using integers 1 to n^2), the magic constant is always M = n(n^2 + 1) / 2. This follows directly from the total of all the numbers: the sum 1 + 2 + ... + n^2 = n^2(n^2+1)/2, and since there are n rows each summing to M, dividing by n gives the formula. For a 3x3 square M = 3 x 10 / 2 = 15. For a 4x4, M = 4 x 17 / 2 = 34. For a 5x5, M = 5 x 26 / 2 = 65. The formula also tells you that a 2x2 magic square is impossible: M would need to be 5, but the four cells 1, 2, 3, 4 cannot each row and column sum to 5 without repeating or omitting a value.
Construction methods: odd, doubly even, and singly even
The algorithm used to build a magic square depends on whether the order is odd, doubly even (divisible by 4), or singly even (even but not divisible by 4). For odd orders (3, 5, 7, 9...) the classical Siamese method, also called the De La Loubere method, places 1 at the top-center and moves diagonally up-right one step at a time, wrapping around the edges. When a cell is already occupied, the next number goes one row down instead. For doubly-even orders (4, 8, 12...) the crossed-diagonals technique fills the grid 1 to n^2 in order, then swaps each entry on a 4x4 diagonal pattern with its complement n^2+1 minus that entry. For singly-even orders (6, 10, 14...) Strachey's method splits the grid into four odd-order quadrants, applies the Siamese method to each with different offsets, then performs a series of column and row swaps to balance the sums.
Reading and verifying the generated square
The generated grid is shown in the breakdown table. Each body row shows the n cell values followed by that row's sum, which equals M. The footer row shows the column sums. To verify by hand: add across every row (n sums), down every column (n sums), and along the two main diagonals (2 sums). All 2n+2 totals must equal the magic constant. If any one of them differs, the square is not magic. This calculator constructs provably correct squares using established algorithms, so the sums will always match.
Magic square properties by order
| Order (n) | Grid size | Magic constant | Sum of all numbers | Type | Distinct solutions |
|---|---|---|---|---|---|
| 3 | 3x3 | 15 | 45 | Odd | 1 |
| 4 | 4x4 | 34 | 136 | Doubly even | 880 |
| 5 | 5x5 | 65 | 325 | Odd | ~275 million |
| 6 | 6x6 | 111 | 666 | Singly even | unknown (vast) |
| 7 | 7x7 | 175 | 1225 | Odd | unknown (vast) |
| 8 | 8x8 | 260 | 2080 | Doubly even | unknown (vast) |
| 9 | 9x9 | 369 | 3321 | Odd | unknown (vast) |
Standard magic squares using integers 1 to n^2. Solutions = distinct fundamental magic squares (excluding rotations/reflections).
Frequently asked questions
Why is a 2x2 magic square impossible?
A 2x2 normal magic square would need to use the numbers 1, 2, 3, and 4. Their total is 10, and with 2 rows the magic constant would have to be 5. But each row and column pair must share exactly two numbers that sum to 5, and there is no way to arrange 1, 2, 3, 4 in a 2x2 grid so that all four lines (two rows, two columns) simultaneously sum to 5. The 1x1 trivial square and all orders 3 and above are achievable, but 2x2 is the unique impossible case.
What does "normal" mean for a magic square?
A normal magic square uses the consecutive integers from 1 to n^2 exactly once each. Non-normal magic squares can use any set of distinct numbers; the magic property (all lines summing to the same constant) still holds but the constant changes. This calculator generates normal magic squares.
How many different 3x3 magic squares are there?
There is only one fundamental 3x3 magic square, the Lo Shu arrangement with 5 at the center. However, because you can rotate it 90, 180, or 270 degrees and reflect it, there are 8 equivalent versions in total. Any 3x3 magic square you encounter is one of these 8.
What is a pandiagonal magic square?
A pandiagonal (or panmagic) magic square is one where not just the two main diagonals but all broken diagonals (wrapping around the edges) also sum to the magic constant. Pandiagonal squares are rarer and harder to construct. The smallest possible is 4x4, and there is no 3x3 pandiagonal magic square. The calculator generates standard (not necessarily pandiagonal) magic squares.
Can the magic constant be calculated without building the square?
Yes. For a normal magic square of order n, M = n(n^2 + 1) / 2 always holds, regardless of which specific arrangement of numbers is used. You do not need to know the actual grid to find M. This formula is the first output shown by this calculator.
Why does the center cell always equal (n^2+1)/2 for odd-order squares?
For any normal odd-order magic square, the center cell is where the Siamese construction naturally places the middle value (n^2+1)/2. It also follows from symmetry: the average of all n^2 consecutive integers is (n^2+1)/2, and in the unique center-symmetric structure of odd-order magic squares, the center cell must equal this average.