[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Generate multiple (as many as desired) magic squares of a specified size.
Background & Techniques
This program doesn't really solve the above problem, but it may be a first step. It will generate what I think are all 115,139 pan-magic squares of order 5. This is 4 times the widely published number of 28,800 so either I'll be revising this page, or several other sites will be revising theirs.
I recently needed some magic squares for another project and realized that I
did not have a generator. In fact, there did not seem to be a Delphi
version available online, so here is my first attempt at one.
A "Magic Square" of odd order N is a square array of integers 1 through N2 with the the property that the sum of integers each row, each column, and each of the 2 diagonals are all equal . The value of the magic sum is for order N is N×(1+N2)/2. Panmagic (also called pandiagonal) squares have the added property that the (2N-2) broken diagonals also add to the magic sum. There are 8 of these for order 5 squares.
There does not appear to be an algorithm for generating all magic squares, even of odd order, which are more amenable to solution than even order squares. Two methods of generating 5x5 magic squares are implemented here. Neither one is very complete even for 5x5 squares (of which there are apparently several million). This program can generate 115,000 or so with the "panmagic" property.
There is a discussion and generalization of the common De La Loubere's algorithm
for generating squares of odd order in Bell and Coxeter's "Mathematical
Recreations and Essays". Starting with an array of blank cells, there are two
rules for generating entries. Starting with "1", apply a specified (a,b)
increment to it's column and row to get the cell for the next sequential
integer. If the target cell is already occupied, a second "jump" (a+a', b+b')
increment rule is applied.
The "Generate" page in the program allows the generated squares to be saved in a text file format for further study.
The "De La Loubere" Algorithm
The "De La Loubere" rule for generating odd order magic squares requires a
"1' in the middle of the top row and consecutive moves generated by moving
forward and up one square so long as the target cell is empty (Normal move
vector V1=(1,-1)). If the target cell is already occupied, place the number one
cell below the
This page is based largely on information provided by Alan Grogono at found
at www.grogono.com/magic/5x5.php. A Latin square of size N contains N different
symbols placed so that each
If we consider the numbers in a magic square running from 0 to N^2-1, they
can be expressed in base N as 2
Grogono uses capital letters for the radix (leftmost) digits and lower case
for the rightmost digit. He claims
Click the button above to generate all of the order 5 pan-magic squares that
I can find. The 576 Greco-Latin squares with 1 in the upper left corner are
translated so that each of the other 24 numbers appear there. Those squares are
then rotated 90 degrees three times, "flipped over" and and rotated three more
times creating 7 additional squares for each.
Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.
There is not much novel programming here.
Most of the problems were mathematical, such as how to manipulate a 2 dimensional array which has been collapsed into a string (5X5 with 3 characters per cell in this case but could be easily generalized):
Also, there are two arrays that are used to generate magic squares using the Greco-Latin technique. I wanted clicking on either one to generate a magic square for the currently selected rows, so a common "OnClick" exit is used. When the user want to generate all possible squares, the same code is called as each entry in the second array is selected for each row in the first array. But to avoid generating the same square twice when the row is changed in the first table, we need to disable the generation. I did this by setting its "OnClick" exit to Nil before starting the generate loop and restoring it when all had been created.
Suggestions for Further Explorations
Copyright © 2000-2018, Gary Darby All rights reserved.