Perplex City and Magic Squares
I vaguely remember hearing about Perplex City when it was first launched, but I was too caught up in just about everything else to take too much notice. I do remember thinking that a worldwide puzzle/scavenger hunt game with an online component sounded right up my alley, but I was disappointed that the “crossover into real-life” events were centered in a country I had never set foot in.
A number of times since then, I’ve been reminded of the game’s existence, most recently when I heard that Perplex City would be having its first official U.S. event right here in San Francisco…on a day when I already had obligations. In spite of my inability to attend the event, there’s been a resurgence in my interest, and last Friday while in Berkeley for a concert I picked up a few packs of “PC” cards from Games of Berkeley.
Whoo boy, the good times are a-startin’. I love myself a good mental workout, and the Perplex City cards provide just that in diverse forms and at varying intensity. From pattern-matching to pop-culture knowledge, logic puzzles, physics problems, political trivia all abound.
Probably my favorite aspect of Perplex City problem-solving so far is scripting solutions to some of the more complicated puzzles. When I was working on a solution for card #098 ‘Magic Square’, I came up with a script which can be used to solve any 4 by 4 magic square, where the rows, columns, and diagonals all add to the same number.
My first attempt was far less than ideal: it randomly arranged the 16 numbers and then tested to see if everything added up properly (i.e. bogosort). When I let it run for ten minutes at a time, it would run through about 12-13 million combinations and not come up with a single solution.
My second attempt used iteration to go through possibilites in a fixed order and guaranteed me a solution eventually…within the next 56 years (seriously, I calculated), if I let the script run constantly, it would check every possible arrangement of numbers for ‘magic squareness’.
My third attempt is the one I finally found success with. Essentially, it’s a modified version of the second script where I run validity tests incrementally instead of all at once after a square is constructed. The spaces within a square are filled in a spiral pattern and rows, columns, and diagonals are tested the moment they’re testable, which results in maximum efficiency.
Where the first script would have required hundreds or possibly thousands of years, and the second script nearly a lifetime, my third script only took about 23 minutes and 35 seconds to find all 924 possible solutions for a 4×4 magic square.
As it turns out, there are 54 unique solutions to the Perplex City Magic Square card.
Oliver 10:18 pm on Tuesday, October 31, 2006 Permalink
Hi, I’m a software engineer, and I find your approach interesting. I wrote a small code as an exercise to solve N x N squares on my spare time. My results for the 4 x 4 were different. I got 7040 solutions in 466 seconds. What i did was I iterated over all the possible permutations. I truncated the trees that will never arrive a solution checking only the partial array as necessary. I believe that you have ommitted a lot of the possible solutions. :)
Matt 8:03 pm on Thursday, March 6, 2008 Permalink
Hey I am also a software engineer and I noticed that you are missing quiet a few answers one being the magic square of
7 2 11 14
12 13 8 1
6 3 10 15
9 16 5 4
Else very interesting and good attempt
Marcel 8:20 am on Wednesday, March 12, 2008 Permalink
Hi,
nice that you are also interested in magic squares and a nice attempt to find solutions.
I see that your program finds 924 solutions for a 4×4 magic square. However, in reality there are lots more.
But there are 880 unique solutions (a unique solution cannot be obtained by rotating and/or mirroring another solution).
I wrote a program in Fortran which can find all solutions for magic squares of any length.
It can be run under DOS or Windows.
You can download it freely at download.com (the name is magic square generator 1.0).
For 4×4 it will find all 880 solutions in about 5 minutes. My approach is comparable with yours by incrementing numbers and testing rows, columns and rows as soon as possible.
However the order I incremented numbers was different (e.g. for 4×4, for other sizes similar):
a b c d
e h i j
f k m n
g l o p
I hope you enjoy the program.
doggy 11:05 pm on Tuesday, September 9, 2008 Permalink
Ummm….. I think its stupid because it didn’t really help me.
Chandrashekhar Joshi 8:15 am on Thursday, January 1, 2009 Permalink
Hi:
I was checking a magic square with following conditions:
x x x 10
x x 9 x
x 8 x x
7 x x x
However I did not get any sucess using the formula given by you. Is there any solution which exists using this combination?
joshua meadows 6:37 pm on Tuesday, March 17, 2009 Permalink
I found one you missed
1st row: 1,15,14,4
2nd row: 12,6,7,9
3rd row: 8,10,11,5
4th row: 13,3,12,16
Thanks though, i like what your doing with this.
Søren Blaabjerg 1:20 am on Tuesday, June 16, 2009 Permalink
I believe, that I myself (not using any formulas or with help of a computer) to have found (so far) no less than 1248 unique “perfect” 4×4 magic squares. The number of unique sets of 8 magic squares, where all the variants can be derived from each other by mirroring and rotation I have found to be 156. (156 x 8=1248)
By perfect 4×4 magic squares I mean squeres where not only do both of the diagonals add up to 34, but so do the numbers in each of the small 2×2 squares at each of the corners, the central 2×2 square, and the sums of the 2 numbers in the middle of the top and the bottom row as well as the sums of the 2 numbers in the middle of the leftmost and rightmost columns.
By the way, I am thinking of writing a small book displaying all of these solution (not dwelving into theories and methods though because it is meant primarily for enjoyment. Do you think, that might have any interest?
Jhansi 12:05 pm on Thursday, July 23, 2009 Permalink
hi i found one more solution which is not available in your list of solutions..
1 14 13 4
12 6 7 9
8 10 11 5
13 3 2 16
Thanks,
Jhansi.
Søren Blaabjerg 7:27 am on Wednesday, July 29, 2009 Permalink
Since my comment above, I have myself found a lot more unique magic squares. I have not yet checked, if all of them are on your list. They might well be.
By the way, I have found, that on your otherwise quite impresive complete list of 4×4 magic square solutions there are in fact repetitions (assuming of course, that magic squares, that can be derived from other magic squares by mirroring and rotation are only counted first time, they occur). At least I have found, that solution
#388: is in fact identical to solution #120:
14 1 8 11 7 6 10 11
15 4 5 10 12 9 5 8
3 16 9 6 13 16 4 1
2 13 12 7 2 3 15 14
Søren Blaabjerg 8:52 am on Wednesday, July 29, 2009 Permalink
In the above comment I by mistake read the numbers underneath instead of the numbers above. Sorry? However I have just discovered another example of duplication. Please take a look at solution #30 and then solution #37!
Danny Dawson 10:05 am on Wednesday, July 29, 2009 Permalink
The 924 solutions I listed are indeed unique, but not rotationally or reflexively so.
Also, as several commenters have pointed out, it seems that my list is not comprehensive. I haven’t taken the time yet to look back at my old code and determine why this is so, but at some point, I certainly should do so.
Thanks to all of you for your comments, and especially to Søren for continuing to work on this puzzle. Even though I haven’t done any work on this myself in a long time, it’s still quite fun to think about. :)
Søren Blaabjerg 3:49 am on Saturday, August 1, 2009 Permalink
Dear Danny
I have just studied your list further and can confirm, that your list is far from comprehensive and indeed also contain quite a number of duplications (i.e. solutions that can be derived through reflexion and/or rotation of other solutions on the list). I you are interested, I might myself have a look on your code :-)
Lawid 4:02 am on Monday, August 10, 2009 Permalink
Hi,
I have set of 100 integers organized in 10*10 table. Only the sums of two rows are different by +1 and -1 compared to the sum required for the magic square. Can you help me in checking if we can get with these numbers a magic square table?
Thanks
Søren Blaabjerg 12:22 pm on Thursday, August 13, 2009 Permalink
Dear Danny
I have studied your list a bit further and found quite a number of omissions and duplicates
as well. In the end I decided to write a small computer program myself in order to find the complete list, and I have just succeeded doing so. As expected the conclusion was, that there are indeed all in all 880 unique solutions or 880 x 8 solutions if you count all rotations and reflexions. I made the program in such a way, that you can choose either and step through all the solutions displayed one by one.
Most of the solutions are not particularly interesting though. For instance I find those, where both the 2 x 2 subsquares at the corners and the subsquare at the center add up to the same sum as the rows, columns and two diagonals particularly pleasing. There are quite a number of those, around 350 unique ones I believe.
gm 3:06 am on Saturday, May 14, 2011 Permalink
Hey guys
Have you been able to solve any EVEN number magic squares?
eg: 6×6, 8×8, etc
There are more than one solutions as for 4×4. Please have a go.
gm
Leo 7:07 pm on Wednesday, September 7, 2011 Permalink
I found a new magic square in my homework.
1 14 8 11
15 4 10 5
12 7 13 2
6 9 3 16
Boris 10:33 am on Wednesday, February 8, 2012 Permalink
I am interesting only in those magic squares that to addition to sums in rows, columns and diagonales also have the same amount in all 4 quarters. One of possible solutions for that is the following one:
14 3 5 12
8 9 15 2
11 6 4 13
1 16 10 7
By the way, it was missing in your 924 solutions (giving that they all are shown in order of the first two numbers).
Tim Dunn 3:08 pm on Friday, April 20, 2012 Permalink
Hi there,
Interesting stuff. I wrote an Excel spreadsheet that finds a solution to 4×4 (not always the same solution)
It uses an evolutionary approach. Start with random arrangement. Generate 10 offspring by swapping two random locations. Calculate the ‘goodness’ of each offspring (sum of differences in all row col diag sums). Use the best offspring as the parent for the next generation. I find that it get a solution in less than 100 generations if it ever gets one. Sometimes it gets stuck in local minima from which there is no escape. To get round this I simply set a limit (150) on generations before reverting to the original arrangement and re-starting.
One interesting thing is that it was not told that the total had to be 34, only that all the totals had to match.
Obviously it would be saner to code it in C or something rather than Excel but at the time I had no access to a compiler.
Chris Cowan 5:26 am on Monday, April 23, 2012 Permalink
i found a one that you have listed as unsovibaule 3 17 16
14 9 11
10 12 7
5 4 18
Rick 1:59 pm on Wednesday, May 9, 2012 Permalink
There was a third solution you didnt identify… that I did for 4×4.. .each equaling 34