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
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 Solver

### What is a magic square? (Definition)

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.

### How to create a magic square?

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).

### How to create a magic square of odd order?

The creation of magic squares of size (3,5,7 etc.) is possible by several methods, the simplest is the so-called 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

### How to create a magic square of even order?

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:

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
becomes
 1 15 14 4 12 6 7 9 8 10 11 5 13 3 2 16

Other methods generally rely on creating odd subsquares.

### How to solve a magic square?

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
translates into equations: $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$

The equations obtained sometimes have several solutions.

### What are the minimal possible sums (magic values)?

The constant values $M$ of the sums of the magic squares have a minimum value (for non-zero 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.

### What are the maximal possible sums (magic values)?

The values can be as large as you want, so the maximum magic sum/value is infinity.

### 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:

 19 17 6 5 21 16 18 4 20
 15 1 26 25 14 3 2 27 13
 8 24 10 12 7 23 22 11 9

### 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:

 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

### What is the Lo-Shu Magic Square?

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

 4Wealth 9Fame 2Relationship 3Family 5Health 7Children 8Wiseness 1Career 6Help/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. 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.

## Cite dCode

The copy-paste 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 2024-06-25, 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!