4 x 4 Magic Square solutions

Great source, thanks, although it's shortening my productivity today.

I did a bit of checking by modifying the code to output each solution as a single line of comma separated values. This allows the use of text utilities to compare the output.

The 1740 solution code has no duplicates, neither does the 880 solution code.
All solutions from the 880 solution code appear in the 1740 solution code.

Further modifying the 880 solutions code, by expanding the seq array to eight arrays representing all rotations/reflections, produces 1740 solutions with no duplicates. These 1740 solutions match the 1740 solutions of the original 1740 solution code. So it appears the 880 solution code does produce unique solutions, none of which are a reflection/rotation of another.
< edit > As @DaveX points out below, I should have written 7040, not 1740. Not sure what I was thinking.

1 Like

7040?

Thanks.

I was looking for other mechanisms of making the 880, and came across a set of 384 "pan-diagonal" magic squares with extra constraints where all of the "broken diagonals" also add up to the sum. They are have extra symmetries where you can rotate them by one column or row (e.g. cols 0123 become cols 3012) and remain pan-magic squares.

This site says you can construct them all from three basic squares:

And wikipedia sort of says it too:

A pandiagonal magic square remains pandiagonally magic not only under rotation or reflection, but also if a row or column is moved from one side of the square to the opposite side. As such, an n×n pandiagonal magic square can be regarded as having 8*n^2 orientations.

With n=4, 8*n^2=128, you get 128 transforms on each of the 3 unique basic pan-diagonal squares to get to the 384 total "most perfect magic squares".

From the set of 880, solution #150 is a "most perfect magic square" with the broken diagonals, the sub-squres

Solution #150 [9 sec]:
 1 12  7 14 
 6 15  4  9 
16  5 10  3 
11  2 13  8 

All these patterns of sums on the #150 solution (and the special 384 of the 880 unique ones) will add up to magic constant "34":

@DaveX , @david_2018
Impressive work guys!

All: I've learned a lot from all contributors, thanks. If anyone's still hungry for more, see this site
Big and Beautiful site

Being the author of the code Terrypin quoted, at first I must apologize that I have wasted so many user's precious time by publishing my half-baked code. And I have to confess I am very slowly getting acquainted to writing in English. You still will find the comments and even variable names and self-defined function names in my codes in German. Anyway, I was greatly impressed by the fact that Mr. Bernard Frenicle de Bessy managed to compile the complete list of 4x4 magic squares even before 1675 (his findings were published posthumously) without access to any electronic support, which animated me to do my very best to speed-up my code.
That is why I shifted the numbers from 1-16 to 0-15 in order to have 16 bits (int16_t) to handle the numbers which are still available. I also modified the sequence of setting values in a way that increased the speed slightly, but confused some readers. But all this is history. You may find a recent and faster version at

If you are more interested in magic squares, may I recommend a book published in 2024 by Holger Danielsson: Magische Quadrate und ihre Konstruktion

(sorry, english version not yet available)
The news about finding the number of all 6x6 magic squares in 2024

(is there any hard-disk to store them all?) is obviously out of reach of any arduino ...

 17 753 889 197 660 635 632

interesting, that is rounded 18E+18

One number 0..35 takes minimal 6 bits. You can easily encode a 6x6 magic square as a 5x5 square so that you need to solve only the last square per row/column. That means you need 25 x 6 bits = 150 bits ~ 19 bytes (almost a factor 2 over the 36 bytes normally) There might be better compression schemes, but I can’t think up a better one so fast.

The largest hard-drive at the moment is 36 TB IIRC, so it can roughly store 2E+12 solutions. There are 18E+18 solutions so you need an array of 9 million of those disks. (and a nuclear plant as a power supply :wink:

So the answer to you question is no, but …. if you could devise an algorithm that can generate the nth magic square on the fly, you do not need a big disk. There exists algorithms to generate the nth permutation but that is unfortunately not enough( by far). There are 36! permutations ~ 38E+40 so only one permutation in 2E+22 is a solution :frowning: