How to Build a Magic Square (Magic Square Math Explained)

A magic square is a grid of numbers where every row, column, and both diagonals add up to the same total. Here is how the math works and how to build one by hand.

What counts as a magic square

A normal magic square is an n×n grid filled with the numbers 1 through n², each used exactly once, arranged so every row, every column, and both main diagonals add up to the same total.

The magic constant formula

The shared total, called the magic constant, is found with M = n(n²+1)/2. For a 3×3 square, that is 3×(9+1)/2 = 15 — the sum of every row, column, and diagonal.

The Siamese method for odd-order squares

For any odd n, place 1 in the top-middle cell, then move diagonally up and to the right for each next number, wrapping around to the opposite edge when you go off the grid. If that target cell is already filled, drop straight down instead and continue from there.

Building a doubly-even square (n divisible by 4)

For n = 4, 8, 12, and so on, first fill the grid in normal reading order from 1 to n². Then swap each number that falls on a diagonal of any 4×4 sub-block with its mirrored position — the result is a valid magic square.

Singly-even squares (n = 6, 10, 14…) are the hardest case

These use more involved methods, like the LUX method, which splits the grid into four odd-order quadrants built separately and then swaps specific numbers between them to correct the totals.

Why magic squares have fascinated mathematicians for centuries

The oldest known example, the 3×3 Lo Shu square, comes from ancient China, and magic squares were later studied independently in India and the Islamic world. Albrecht Dürer's 1514 engraving Melencolia I famously features a 4×4 magic square that hides the artwork's creation year, 1514, in its bottom row.

The construction method depends on the square's order

There is no single method that builds every size of magic square — the algorithm differs depending on whether n is odd, doubly even (divisible by 4), or singly even (divisible by 2 but not 4). That split is exactly why dedicated generators exist rather than one universal formula.

Frequently Asked Questions

Can every size of magic square be built?

A 2×2 magic square is mathematically impossible, but for any n of 3 or greater, a magic square can be constructed — the method just depends on whether n is odd, doubly even, or singly even.

Do all magic squares use the numbers 1 through n² in order?

The classic "normal" magic square does. Non-normal variants exist too, using other sets of numbers (like multiples of a base number) that still produce equal row, column, and diagonal sums without being consecutive integers.