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 grid of numbers arranged so that the sum of each row, each column and its diagonals are all equal to the same value, called the magic sum (or magic constant).
Magic squares are popular for their simplicity and mathematical properties.
A magic square arranges numbers so that their placement follows construction rules that ensure that the sums of rows, columns, and diagonals equal the magic sum.
Construction methods vary depending on the order of the square (number of rows/columns).
The creation of magic squares of size (3,5,7 etc.) is possible by several methods, the simplest is the socalled Loubère method (staircase method):
Place 1 in the center of the first line, then the following numbers in the box located diagonally at the top left. If the box is outside the square, imagine that the square wraps around itself and continue on the other end (as if the left column were to the right of the right column, and the bottom line, at the above the top line). If the target square is occupied, then continue directly below the last filled square.
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 
Creating magic squares of even order (4,6,8, etc.) is more complicated and the methods are not universal.
Symmetry method for squares of size 4:
— Place the numbers naturally from 1 at the top left to 16 at the bottom right.
— Replace the numbers on the sides (2, 3, 5 and 9) by their central symmetry (relative to the center of the square).
Example:
 becomes 

Other methods generally rely on creating odd subsquares.
The magic way to solve magic squares is to set the equations that govern each row and column, with unknowns. The constraints being that the unknowns are all different positive integers (distinct including numbers already placed).
Example: The magic square of magic value $ X $
a  b  c 
d  e  f 
g  h  i 
The equations obtained sometimes have several solutions.
The constant values $ M $ of the sums of the magic squares have a minimum value (for nonzero integer positive values).
$$ M = n(n^2+1)/2 $$
For a size 3x3, the minimum constant is 15, for 4x4 it is 34, for 5x5 it is 65, 6x6 it is 111, then 175, 260, …
Any lower sum will force the use of either negative numbers or fractions (not whole numbers) to solve the magic square.
The values can be as large as you want, so the maximum magic sum/value is infinity.
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 semipanmagic 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. Except explicit open source licence (indicated Creative Commons / free), the "Magic Square" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "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.) and all data download, script, or API access for "Magic Square" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.
The copypaste of the page "Magic Square" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
Magic Square on dCode.fr [online website], retrieved on 20240625,