Skip to main content
Skip to calculator
Advertisement

Last updated: August 1, 2026

Magic Square Calculator

Quick Answer

This magic square calculator builds normal magic squares of order 3 or higher, computes the magic constant, and returns row, column, and diagonal sums to verify the construction.

A magic square uses the numbers one through n squared so each row, column, and main diagonal add to the same magic constant.

Helpful
Not helpful
Save as image
Share
Embed
Cite
Write feedback

Formula

magicConstant = order × (order² + 1) / 2

Where:

  • n=Order of the square
  • M=Magic constant for every row, column, and diagonal
  • 1 ... n²=The consecutive integers placed into the square
Magic Square IllustrationA 3 by 3 magic square where every row, column, and diagonal sums to 15.Magic SquareEvery row, column, and main diagonal matches the same magic constant816357492151515151515
The classic Lo Shu square shows the core idea: no matter which row, column, or diagonal you sum, the total is the same.

Worked Examples

Classic 3×3 square

Generate the Lo Shu magic square for order 3.

  1. 1Use the Siamese method for odd order.
  2. 2Place 1 in the top middle cell.
  3. 3Move up-right, wrapping around the edges.
  4. 4Each row, column, and diagonal sums to 15.
Final Answer: Magic constant = 15

Doubly even 4×4 square

Generate a 4×4 square using the complement pattern.

  1. 1Fill the square with 1 through 16.
  2. 2Reverse values in non-diagonal cells of each 4×4 block.
  3. 3Verify that every row and column sums to 34.
  4. 4Both main diagonals also total 34.
Final Answer: Magic constant = 34

Singly even 6×6 square

Combine four odd-order sub-squares and swap columns.

  1. 1Build a 3×3 odd-order seed square.
  2. 2Copy it into four quadrants with offsets.
  3. 3Swap the required left and right columns between quadrants.
  4. 4Every verification sum becomes 111.
Final Answer: Magic constant = 111

Larger odd square

Produce a 5×5 square to inspect the general rule.

  1. 1Apply the Siamese method repeatedly.
  2. 2Wrap around when moving beyond edges.
  3. 3Move down one cell when the target is occupied.
  4. 4The constant becomes 65.
Final Answer: Magic constant = 65

Introduction

A magic square arranges the consecutive integers from 1 to n² so every row, every column, and both main diagonals add to the same value. Different construction rules apply depending on whether the order is odd, singly even, or doubly even.

What a Magic Square Is

A magic square is a structured number grid with a single shared sum across key lines.

  • Uses every integer from 1 to n² exactly once

  • Rows match the same sum

  • Columns match the same sum

  • Both main diagonals match the same sum

Magic Constant Formula

The shared line sum depends only on the order of the square.

  • Formula: n(n² + 1) / 2

  • For 3×3 the constant is 15

  • For 4×4 the constant is 34

  • For 6×6 the constant is 111

Odd-Order Construction

Odd sizes use the Siamese method, which moves up-right and wraps around the grid.

  • Start in the top middle cell

  • Move up-right after each placement

  • Wrap around the edges

  • If occupied, move down one cell instead

Even-Order Construction

Even sizes split into doubly even and singly even cases because they need different algorithms.

  • Doubly even means divisible by 4

  • Singly even means divisible by 2 but not 4

  • Doubly even uses a complement pattern

  • Singly even uses quadrant swaps

How to Use the Calculator

Enter the order and inspect the generated matrix plus all verification sums.

  • Choose an integer order of at least 3

  • Generate the square

  • Check row and column totals

  • Use flattened output to copy the values elsewhere

Why Verification Matters

The row, column, and diagonal checks confirm the construction is correct.

  • Catches copy mistakes instantly

  • Helpful for classroom use

  • Useful when comparing algorithms

  • Confirms edge-wrapping and swaps worked

Practical Notes

Larger squares remain deterministic, but the output gets bigger quickly.

  • The algorithm always returns the same square for the same order

  • No random placement is involved

  • Order must be a whole number

  • Very large squares are harder to inspect manually

FAQs

What is the smallest magic square this calculator generates?

Order 3 is the smallest supported size because order 2 has no normal magic square.

Does the calculator support even orders?

Yes. Doubly even and singly even orders are both supported in addition to odd orders.

Why do all rows have the same sum?

The construction algorithms place the integers so every key line matches the magic constant exactly.

Can I use negative numbers or custom ranges?

No. This calculator generates normal magic squares using the consecutive integers from 1 to n².

What is a doubly even order?

A doubly even order is divisible by 4, such as 4, 8, or 12.

What is a singly even order?

A singly even order is divisible by 2 but not by 4, such as 6 or 10.

Is the 3×3 result always the Lo Shu square?

This implementation always returns the same deterministic orientation of the classic Lo Shu arrangement.