Search for a tool
Magic Square

Tool to generate magic squares size N, kind of matrices composed of distinct integers set such as the sum of any row or column are equal.

Results

Magic Square -

Tag(s) : Number Games, Fun/Miscellaneous, Arithmetics

Share
Share
dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!


Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!


Feedback and suggestions are welcome so that dCode offers the best 'Magic Square' tool for free! Thank you!

Magic Square

Magic Square Generator





Magic Square Solver


Loading...
(if this message do not disappear, try to refresh this page)




Answers to Questions (FAQ)

What is a magic square? (Definition)

A magic square is a square grid of distinct numbers, usually integers, arranged so that the sum of each row, each column, and the two main diagonals is the same.

This common value is called the magic sum (or magic constant).

How to compute the magic constant?

In the case of a standard magic square containing the integers from $ 1 $ to $ n^2 $, the magic constant is $ M = \frac{n(n^2+1)}{2} $.

How to create a magic square?

Creating a magic square consists in arranging numbers in an $ n \times n $ grid so that each row, column, and diagonal has the same sum.

Construction methods depend on the parity of $ n $:

— if $ n $ is odd: use a method such as de la Loubère's method

— if $ n $ is divisible by $ 4 $ (doubly even order): use symmetry-based methods

— otherwise (singly even order): use hybrid methods based on smaller sub-squares

How to create a magic square of odd order?

For a magic square of odd order (3x3, 5x5, 7x7, …), the most common method is de la Loubère's method (also called the Siamese method):

— Place the number $ 1 $ in the middle of the top row.

— Place each next number in the cell diagonally up and to the right.

— If this position goes outside the square, wrap around to the opposite side.

— If the target cell is already occupied, place the number directly below the last filled position.

Example: Staircase method generating a magic square of order 3:

816
357
492

Example: Staircase method generating a magic square of order 5:

17241815
23571416
46132022
101219213
11182529

How to create a magic square of doubly even order?

For a magic square of doubly even order (4x4, 8x8, …), a common method is the crossed-diagonals method:

— Fill the square with the numbers from $ 1 $ to $ n^2 $ in increasing order, left to right and top to bottom.

— Divide the square into $ 4 \times 4 $ sub-squares and draw both main diagonals in each sub-square.

— Keep the numbers on these diagonals unchanged.

— Replace all other numbers (those not on the diagonals) with their complement to $ n^2 + 1 $ (that is, counting backwards from $ n^2 $).

Example: Diagonal method generating a magic square of order 4:

1234
5678
9101112
13141516
115144
12679
810115
133216

How to create a magic square of singly even order?

For a magic square of singly even order (6x6, 10x10, …), Strachey's method is the most common:

— Divide the square into four equal sub-squares (A top-left, B bottom-right, C top-right, D bottom-left).

— Fill each sub-square as an odd-order magic square (using the staircase method), assigning consecutive ranges of numbers.

— Swap certain cells on the left side between quadrants A and D, with a specific offset on the middle row.

— Swap a defined number of right-side columns between quadrants C and B to balance the sums.

Example: Strachey method generating a magic square of order 6 in 2 steps:

816261924
357212325
492222720
352833171015
303234121416
313629131811
3516261924
3327212325
3192222720
82833171015
30534121416
43629131811

How to solve a magic square?

Solving a magic square consists in determining the missing values while respecting the sum constraints. One approach is to introduce variables and write the equations for rows, columns, and diagonals of the resulting matrix.

Example: For a $ 3 \times 3 $ square with magic constant $ X $: $$ a+b+c = X \\ d+e+f = X \\ g+h+i = X \\ a+d+g = X \\ b+e+h = X \\ c+f+i = X \\ a+e+i = X \\ c+e+g = X $$

Solutions must use distinct values, most often positive integers.

What are the minimal possible sums (magic values)?

For a magic square using the integers from $ 1 $ to $ n^2 $, the magic sum is fixed and minimal: $ M = \frac{n(n^2+1)}{2} $

Example: $ 3 \times 3 $: $ 15 $, $ 4 \times 4 $: $ 34 $, $ 5 \times 5 $: $ 65 $

Any attempt to obtain a smaller sum requires using negative or non-integer numbers.

What are the maximal possible sums (magic values)?

There is no maximum value for the magic sum if the numbers are not constrained.

Indeed, multiplying all entries of a magic square by a constant $ k $ multiplies the magic sum by $ k $.

Therefore, the maximal magic sum is unbounded (can be arbitrarily large).

How many magic squares are there?

The number of magic squares depends on the order $ n $:

— order $ 3 $: there is $ 1 $ fundamental magic square (and $ 8 $ including rotations and symmetries)

— order $ 4 $: there are $ 880 $ distinct magic squares (excluding symmetries)

For larger orders, the number grows extremely fast and becomes difficult to compute.

What is a panmagic square?

A panmagic square, also called a pandiagonal square, is a special type of magic square. Unlike traditional magic squares, where only rows, columns, and major diagonals have equal sums, a panmagic square has an additional property: the sums of the numbers along all its diagonals (including minor diagonals) are equal. also equal to the magic sum.

Is there a Magic Square in 3D (Magic Cube)?

Yes, there are magic cubes, their magic value is $$ M = n(n^3+1)/2 $$ (which may or may not have magic diagonals)

Example:

19176
52116
18420
15126
25143
22713
82410
12723
22119

What is the Franklin Square?

Franklin's square, published in 1769 by Benjamin Franklin, is a semi-panmagic square with a magic constant of 260.

Example:

526141320293645
143625146353019
536051221283744
116595443382722
555871023263942
98575641402524
506321518313447
161644948333217

What is the Lo-Shu Magic Square?

This is a 3x3 magic square used in Feng Shui which is represented as well

4
Wealth
9
Fame
2
Relationship
3
Family
5
Health
7
Children
8
Wiseness
1
Career
6
Help/Friends

What is the Kaldor Magic Square?

Kaldor's magic square is a square used in economics, which has nothing to do with digits or numbers of mathematics but rather with concepts from economic policy.

Source code

dCode retains ownership of the "Magic Square" source code. Any algorithm for the "Magic Square" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Magic Square" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Magic Square" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.

Cite dCode

The content of the page "Magic Square" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).

Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).

To cite dCode.fr on another website, use the link: https://www.dcode.fr/magic-square

In a scientific article or book, the recommended bibliographic citation is: Magic Square on dCode.fr [online website], retrieved on 2026-05-08, https://www.dcode.fr/magic-square

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Magic Square' tool for free! Thank you!


https://www.dcode.fr/magic-square
© 2026 dCode — The ultimate collection of tools for games, math, and puzzles.
 
Feedback