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.
Magic Square - dCode
Tag(s) : Number Games, Fun/Miscellaneous, Arithmetics
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!
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).
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} $.
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
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:
| 8 | 1 | 6 |
| 3 | 5 | 7 |
| 4 | 9 | 2 |
Example: Staircase method generating a magic square of order 5:
| 17 | 24 | 1 | 8 | 15 |
| 23 | 5 | 7 | 14 | 16 |
| 4 | 6 | 13 | 20 | 22 |
| 10 | 12 | 19 | 21 | 3 |
| 11 | 18 | 25 | 2 | 9 |
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:
| → |
|
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:
| → |
|
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.
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.
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).
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.
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.
Yes, there are magic cubes, their magic value is $$ M = n(n^3+1)/2 $$ (which may or may not have magic diagonals)
Example:
|
|
|
Franklin's square, published in 1769 by Benjamin Franklin, is a semi-panmagic square with a magic constant of 260.
Example:
| 52 | 61 | 4 | 13 | 20 | 29 | 36 | 45 |
| 14 | 3 | 62 | 51 | 46 | 35 | 30 | 19 |
| 53 | 60 | 5 | 12 | 21 | 28 | 37 | 44 |
| 11 | 6 | 59 | 54 | 43 | 38 | 27 | 22 |
| 55 | 58 | 7 | 10 | 23 | 26 | 39 | 42 |
| 9 | 8 | 57 | 56 | 41 | 40 | 25 | 24 |
| 50 | 63 | 2 | 15 | 18 | 31 | 34 | 47 |
| 16 | 1 | 64 | 49 | 48 | 33 | 32 | 17 |
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 |
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.
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.
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:
In a scientific article or book, the recommended bibliographic citation is: Magic Square on dCode.fr [online website], retrieved on 2026-05-08,