A famous painting of artist Albrecht Duerer contains a magic square with 4x4 cells.
If you take a close look at the upper right corner of the picture shown above, you will find this:
It has been proven long ago that 880 such squares exist, if you rotate and reflect them even 7040 of them. Generating all of them with brute force can be a time-consuming job.
Why is it crucial to look for a better algorithm? You might try all numbers from 1 to 16 at all positions from 1 to 16, which results in 16^16 = 1.8 * 10^19 trials. Slightly better if you avoid using numbers more than once. Still you end up with checking 16*15*14*13*12*11*10*9*8*7*6*5*4*3*2*1 = 16! =
20,922,789,888,000 configurations, which you all have to check for the magical sum, and you never will see the end. Luckily, I found an algorithm published by K. Muroi (Japan) in 2004, which with some modifications performs really fast. I uploaded it to an Arduino Pro-Mini as shown in the picture. The output is sent to a TFT display, where each solution is shown for a short time. The sketch creates one solution after the other, gives a short beep when a new solution was found and waits for a few moments to enable watching. It also shows its unsuccessful trials but removes them as soon as it can. All solutions are sent to the Serial monitor where they can be extracted.
Actually, I don't know what is more interesting: watching all the numbers passing by, or taking a look at the algorism itself.
Improved VersionLately, I was inspired by an article by K.Muroi which I found at https://www.netstaff.co.jp/. Muroi points out there is only a limited number of possible partitions of any sum, and you only have to check these partitions, which reduces the number of trials dramatically.
You will find this version as a zip-file in the attachments. This sketch is written for the 2.2" TFT, but can easily modified for any other. As it is running very fast, I added controls to pause or even stop it for a while.
Some footnotes:#1: A lot of articles and even books have been written about this picture. There is no need to repeat them all.
#2: Look at the first number in row 2:
Obviously, there was no correction tape (Tipp-ex) for copper engravings at that time. In case of mistakes you had to start from the scratch.
And please note that copperplate engravings must always be designed mirrored, which makes it more difficult.
#3: You might have noticed that the figures in the bottom row show the artists initials (in the corners 4 = 'D' and 1 = 'A', there was no ASCII code at that time) and in the center the year when the artwork was created. If you wanted to imitate that trick you had to wait at least until [2] [1] [15] and one of your initial letters must be 'P'. This one will work:
7 12  6  9
11  8 10  5
14 13  3  4
 2  1 15 16Good luck! The new version even supports an option for output directly as letters.
#4: If you want to operate the project using a power bank, be warned: some power banks are programmed to require a certain minimum current and will switch off if the actual current is below. The magic square project will just take less than 50 milli amps. As the power banks manuals usually don't tell you about this you just have to check yourself.
#5: The code provided will not compile using ARDUINO UNO R4 as the TFT library has not got an update for it. But even if it had been done, SPI support for R4 is much slower than for R3.


_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)


Comments