Recreational Math II : Even-Ordered Magic Squares: 4×4 part 2

Hits: 108

Click here for my previous article on 4×4 Magic Squares.
Click here for an even earlier article on 4×4 Magic Squares.

Much of the work from that article came from an Excel spreadsheet, which I forgot to save. No big deal, I re-constructed the 24 squares again using Grogono’s magic carpets. But as I was doing this I was finding to my horror that these new magic squares – all proven to be pan-magic — were different from the 24 squares in the previous article. What was notably absent was the number 16 in the upper left and the 1 in row 3, column 3, which occured in all 24 of the squares generated yesterday. In fact, what had happened instead is that the 1 and the 16 switched places. Weird.

So, I took a closer look. And I found out that in the last article, I based my carpets on a scheme I found on the internet. Again, it looks like this:

This was not what I got from the Grogono site. The Grogono site had a scheme more like this:

Every placeholder and zero are switched. What had a zero now has a placeholder, and what was a placeholder now has a zero. While the “pan-magicness” was not affected, we appeared to obtain new magic squares.

But what if I picked and chose which carpets to invert, and which to leave as-is. Would the result still be magic? That is, could I still get pan-magic if I chose to invert only one random carpet, or two, or three random carpets?

From what I have investigated, it appears that in all cases, yes, that is what will happen. And when I did that, I began to notice squares which were rotated or flipped versions of other existing squares. Below are the 16 squares generated from square “1248” from the previous day’s posting, by inverting one, two, three or all four magic carpets in all possible permutations, never changing the ordering of the carpets. All of these are pan-magic.

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

Changing the ordering of the carpets makes no difference in the resulting magic square, as addition is commutative. Throughout these past two postings, I have always kept the carpets in one order while trying out all possible substitutions of the numbers 1, 2, 4, and 8 on them. The above set are variations on 1248 with each new variant labelled from 1 to 16, which labels are highlighted in yellow.

Keeping the ordering of the carpets the same as in the two illustrations at the start of this article, let a “1” represent an inverted, Grogono carpet, and a “0” represent a member from the first set of carpets. So, a “1111” would repsent the fact that all Grogono carpets were used, and “0000” represent the idea that all carpets were used as per the first illustration, or as seen in the last posting. Something like “0100” would tell you that only the second carpet was the inverted Grogono carpet, and the rest were the original carpets I used. I wish I could say that the 16 magic squares illustrated above went with the ordering of the binary numbers, but instead I began my spreadsheet experimentally with some random choices, then proceeded to fill out the missing combinations as I went. What follows is the actual ordering of those squares.

0000 \rightarrow 0 (the original 1248 square from the last post, not numbered)
1000 \rightarrow 1
1100 \rightarrow 2
1110 \rightarrow 3
1111 \rightarrow 4
1000 \rightarrow 5
0100 \rightarrow 6
0110 \rightarrow 7
0111 \rightarrow 8
0101 \rightarrow 9
0011 \rightarrow 10
0001 \rightarrow 11
1010 \rightarrow 12
1001 \rightarrow 13
1100 \rightarrow 14
1011 \rightarrow 15

I count 24 \times 16 = 384 magic squares, while being aware that many of them are rotations or reflections, possibly of the first 24. For example, squares 1 and 7 is a reflection; 2 and 13 is a rotation, as is 3 and 15. So, I am quite sure that 384 will not be anywhere close to the count when rotations and reflections are taken into consideration.

The new magic squares I have obtained are thus, using the Grogono carpets:

1248 1284 1248 1482
1 15 4 14 1 15 4 14 1 15 6 12 1 15 6 12
12 6 9 7 8 10 5 11 14 4 9 7 8 10 3 13
13 3 16 2 13 3 16 2 11 5 16 2 11 5 16 2
8 10 5 11 12 6 9 7 8 10 3 13 14 4 9 7
1824 1842 2148 2184
1 15 10 8 1 15 10 8 1 14 4 15 1 14 4 15
14 4 5 11 12 6 3 13 12 7 9 6 8 11 5 10
7 9 16 2 7 9 16 2 13 2 16 3 13 2 16 3
12 6 3 13 14 4 5 11 8 11 5 10 12 7 9 6
2418 2481 2814 2841
1 14 7 12 1 14 7 12 1 14 11 8 1 14 11 8
15 4 9 6 8 11 2 13 15 4 5 10 12 7 2 13
10 5 16 3 10 5 16 3 6 9 16 3 6 9 16 3
8 11 2 13 15 4 9 6 12 7 2 13 15 4 5 10
4128 4182 4218 4281
1 12 6 15 1 12 6 15 1 12 7 14 1 12 7 14
14 7 9 4 8 13 3 10 15 6 9 4 8 13 2 11
11 2 16 5 11 2 16 5 10 3 16 5 10 3 16 5
8 13 3 10 14 7 9 4 8 13 2 11 15 6 9 4
4812 4821 8124 8142
1 12 13 8 1 12 13 8 1 8 10 15 1 8 10 15
15 6 3 10 14 7 2 11 14 11 5 4 12 13 3 6
4 9 16 5 4 9 16 5 7 2 16 9 7 2 16 9
14 7 2 11 15 6 3 10 12 13 3 6 14 11 5 4
8214 8241 8412 8421
1 8 11 14 1 8 11 14 1 8 13 12 1 8 13 12
15 10 5 4 12 13 2 7 15 10 3 6 14 11 2 7
6 3 16 9 6 3 16 9 4 5 16 9 4 5 16 9
12 13 2 7 15 10 5 4 14 11 2 7 15 10 3 6
How many 4×4 magic squares are there?

In 1933, a mathematician who taught at UC Berkeley named Derrick Norman Lehmer published a short paper in the October 1933 edition of the Bulletin of the American Mathematical Society, entitled “A Complete Census of 4×4 Magic Squares”. In this paper, he deals with magic squares generally, not just the pan-magic ones. He asserted that there are 539,136 magic squares of order 4 in total.

His way of counting involves the ability to normalize them by rearranging the numbers in a methodical way which shows that squares showing a different normalization cannot be transformed by shuffling columns or rows. It turns out that there are 468 normalized squares of order 4. Each of these 468 normalized squares has 2(4!)^2 = 1152 possible shufflings of its rows and columns to produce different squares. And thus, 468 \times 1152 = 539,136.

As for order-4 pan-magic squares, there are 52 ways to obtain a total of 34. The diagram below came from a Ted Talk by mathematician Michael Daniels, and illustrates the 52 ways to obtain the total.

Grogono also offers a diagram where you can see the effects of changing the numbers on a 4×4 magic square, the inversion of the carpets, and so on. You just have to remember that for his squares, the numbering starts from 0 and the magic number is 30. So, that to get the magic number 34, you need to add 1 to each position in the magic square.

Recreational Math II: Even-ordered Magic Squares: 4×4

Hits: 105

I had been going on now for quite a while about magic squares of odd-order, particlularly those magic squares whose order is a prime number of 5 or more. The algorithm for creating 5×5, 7,7, 11×11, 13,13 and other prime-ordered magic squares are all pretty similar, based on a sum of two squares and what might be called a “knight’s tour” to the right for the first one, and to the left for the second one.

The pattern breaks when you get to squares of even order, or of composite order. So, 4×4 has many pan-magic squares, but most methods I have observed appear to be brute force. And even after I have read about a method for constructing such squares, I have been often disappointed by the low-yielding nature of the algorithms they suggest.

The methods I have described for a 7×7 construction, for example, can yield (71)^2 = (5040)^2 = 25,401,600 possible magic squares. And the larger the prime-ordered square, the greater the possibilities. By the time we get to 13×13, the number of squares becomes (13!)^2 = (6,227,020,800)^2 = 38,775,788,043,632,640,000 or over 38 quintillion. Counting to such a number would take more than 1.2 trillion years, hundreds of times longer than the age of the universe. And we are just getting warmed up. After all, 13×13 isn’t really that big. But I think by now I can impress on you what a “high-yielding” algorithm can do. It must be stated that there appear to be no magic squares beyond 5×5 that are pan-magic. The rest appear to be normal magic, which I will distinguish shortly.

The methods I will now describe in the coming articles, those of composite order of 4 or more, are generally low-yielding. We will start with order 4. The one I learned about for a 4×4 square was by visiting Grogono.com, a site created and run by Alan Grogono, a professor of anesthesiology at the Tulane University School of Medicine, located in New Orleans, Louisiana. His foray into this branch of mathematics is proof that non-mathematicians are as capable as anyone of spending more time in recreational math than they or their spouses will care to admit.

Grogono states that all 4×4 squares are variations of the same three squares. This is probably uncharitable, since he only refers to pan-magic squares, and that is quite a high bar. He does not appear to count the possibility of squares that are fully magic (or a normal level of magic), but not pan-magic. Normal magic in a 4×4 square will have unique digits from 1 to 16 in some randomized order, which nevertheless add to 34 in each of its rows, columns and diagonals. Pan-magic goes beyond this and offers multiple symmetrical ways of getting the total, in addition to normal magic.

So, I have a strong hunch that if we ignore pan-magic and stick with normal magic, I think I can state cautiously that we will obtain a number of unique magic squares greater than 3.

The Magic Carpet Method

But first, I wll mention the “magic carpet” technique that Grogono describes for the creation of his randomized 4×4 pan-magic squares. The magic carpet method has been around possibly for centuries, probably as long as magic squares have been around. And if we count China, it would have been for at least a millenia. But “magic carpet” for me conjures up images of Arabic lore and mythology. Indeed, magic squares have been known to Middle Eastern and South Asian culture for centuries as well. For all of its renown worldwide, no one has yet thought of a use for magic squares. It remains a staple of recreational mathematics.

A 4×4 pan-magic square requires four 4×4 magic carpets, or numbers patterned in a 4×4 matrix which resemble the weave of a carpet. Each of the carpets have a different “weave”:

The four magic carpets, expressed as matrices.

Each of a, b, c, and d represents a specific number, namely a power of 2. We get to decide which of a, b, c, or d gets to be the numbers 1, 2, 4, or 8. Once filled out, we add all four matrices to obtain the 4×4 magic square we want. But this gets us a magic number of 30, not 34, and the numbers will go from 0 to 15 rather than 1 to 16. If we add 1 to each number, or rather, add a matrix filled with 1’s, that fixes the problem, and the numbers 1 to 16 are restored, and 34 becomes the magic number.

The fact that the 4×4 magic squares depends on the ordering of four numbers means that potentially there are 4!, or 24 possible magic squares. So, it is not a great yield. Grogono tells us that even these squares boil down to the rotations or reflections of the same 3 pan-magic squares.

This caused me to have a look at the magic squares in question to see if there was any truth to this. For your edification, here are the 24 magic squares below:

8421 8412 8241 8214
16 9 6 3 16 9 7 2 16 9 4 5 16 9 7 2
5 4 15 10 5 4 14 11 3 6 15 10 3 6 12 13
11 14 1 8 10 15 1 8 13 12 1 8 10 15 1 8
2 7 12 13 3 6 12 13 2 7 14 11 5 4 14 11
8142 8124 4821 4812
16 9 4 5 16 9 6 3 16 5 10 3 16 5 11 2
2 7 14 11 2 7 12 13 9 4 15 6 9 4 14 7
13 12 1 8 11 14 1 8 7 14 1 12 6 15 1 12
3 6 15 10 5 4 15 10 2 11 8 13 3 10 8 13
4281 4218 4182 4128
16 5 4 9 16 5 11 2 16 5 4 9 16 5 10 3
3 10 15 6 3 10 8 13 2 11 14 7 2 11 8 13
13 8 1 12 6 15 1 12 13 8 1 12 7 14 1 12
2 11 14 7 9 4 14 7 3 10 15 6 9 4 15 6
2841 2814 2481 2418
16 3 10 5 16 3 13 2 16 3 6 9 16 3 13 2
9 6 15 4 9 6 12 7 5 10 15 4 5 10 8 11
7 12 1 14 4 15 1 14 11 8 1 14 4 15 1 14
2 13 8 11 5 10 8 11 2 13 12 7 9 6 12 7
2184 2148 1842 1824
16 3 6 9 16 3 10 5 16 2 11 5 16 2 13 3
2 13 12 7 2 13 8 11 9 7 14 4 9 7 12 6
11 8 1 14 7 12 1 14 6 12 1 15 4 14 1 15
5 10 15 4 9 6 15 4 3 13 8 10 5 11 8 10
1482 1428 1284 1248
16 2 7 9 16 2 13 3 16 2 7 9 16 2 11 5
5 11 14 4 5 11 8 10 3 13 12 6 3 13 8 10
10 8 1 15 4 14 1 15 10 8 1 15 6 12 1 15
3 13 12 6 9 7 12 6 5 11 14 4 9 7 14 4

Each square is labelled with a 4-digit number denoting, in order, the values of a, b, c, and d used in the magic carpets in constructing each square. I was able to order the squares from the highest 4-digit number to the lowest, to show there are no duplicates.

Asside: The missing square: The Dürer Square

Click here for a previous article I wrote on the Dürer Square.

On a side note, I found that the 4×4 magic square created by Albrecht Dürer and engraved on his 1514 Melancholia carving does not appear to be included in the above set of 24. This is one example of a square that is not pan-magic. Here is the Dürer square:

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

The Dürer square is fully magic (all rows, columns and both diagonals add to 34), and in addition, you can obtain 34 by adding the four corner squares, and again by adding the four central squares. But there is no other way to obtain a total of 34 that uses a pattern with any kind of symmetry that would cause us to group it among the pan-magic squares.

The above square follows the pattern of having 16 on  the upper left, but in addition has the year of the engraving, “1514” at the centre squares of the bottom row. It is sort of like the 2418 square in the above series, but just sort of, and without the distinctive “1514”. It also bears some resemblance to the square 2814. But you will find there will be numbers that break symmetry, and so you can’t just shuffle a column or two to get the Dürer square. If you follow the numbers 1 to 16 in the Dürer square, they appear to follow a magic carpet pattern of some kind, but it doesn’t seem quite like what we did.

In the above series of 24 squares, we can see right away that the squares are unique, since a “16” consistently appears on the upper left corner, precluding the idea that any of these squares are mere rotations or reflections of each other. The only reflection I would imagine to be the case might be reflections along the main diagonal, which extends from the upper left to the lower right. There is another consistency in all of these squares in that they also possess a “1” in the third row, third column.

I can make out adjacent pairs of squares with the same main diagonal, where the other diagonal in the second is the reversal of the first. This is seen throughout the 24 squares, for example the first two squares 8421 and 8412 have 16, 4, 1, and 13 as their main diagonal, while the opposing diagonal in 8421 is 3, 15, 14, 2, which is the reversal of the square 8412. Since I have not seen a pair of squares where the rest of the number follow suit to produce such a reflection (known as a “transposition” in matrix math), this just remains an interesting quirk.

However, Grogono went to the extent of defining any magic square with identical columns as “the same”, even if their ordering of those columns are different. This might be justified from the thinking of matrices as vector systems. Thus, shifting columns is merely just shuffling vectors around, and if these were indeed vector systems, all shufflings of four equations (which these vectors would represent) in four unknowns would yield the same solutions. Thus, it can be argued, matrices with shifted columns are “the same”. Examples I have been able to spot are 4281 and 2481. If you look, you begin to find them everywhere, which is why Grogono was able to reason that there were only 3 squares whose columns are truly unique and “non-shufflable”, if there is such a phrase. All the rest are shufflings of these three.

But I have a problem with this in that, objectively speaking, these are not vector systems. This is just a number game where numbers are distributed in a grid of some kind. It might be helpful to think of this as a matrix, or a table, but the fact remains that this is just a number game, and no one is trying to say these are vector systems or linear systems in four dimensions. I find matrix terminology helpful as well, but we can’t get carried away into the recesses of linear algebra, since that is not what this is about.

What most people who dabble in magic squares mean when they say two squares are “the same” is that if you rotate or reflect the magic square and get the same as another square, then they are the same. I offer a quick  example with 3×3 squares:

4 9 2 8 3 4 2 9 4
3 5 7 1 5 9 7 5 3
8 1 6 6 7 2 6 1 8

The middle square is a rotation of the left square. The right square is a reflection of the left square. If we simply either rotate or reflect the other two squares we always get the square on the left. This is why it has been widely stated that only one unique 3×3 magic square exists. According to Grogono, there are 8 variations of this one square, all being rotations or reflections of each other. I will leave it to the reader to find the other five.

So, when someone who knows about magic squares says that squares are the same, they are referring to something “visual” done to it, so that the arrangements of numbers will always be the same relative to each other.

The above 3×3 squares can’t be shuffled, since “9 5 1” and “7 5 3” will always have the immutable property of being either the middle row or the middle column, with “5” always occupying the center square. So, any “shuffling” in a 3×3 is really a reflection, since the outer rows or columns are the only things you are allowed to shuffle. Also notice that the diagonal numbers are immutable as well: “6 5 4” and “8 5 2” are the only game in town for these diagonals. Indeed, rotations and reflections of squares shouldn’t be counted as different, because addition is commutative: 6+5+4 = 4+5+6, for example. Adding the same 3 numbers will get the same total.

In larger squares, if shuffling is observed, I would argue that the squares can be seen as different. The center number, if there is one, can change, and there are no immutable rows or columns that we saw in the 3×3 squares. It still must be conceded that the rows and columns are “the same” due to addition’s commutative property. If you shuffle the columns, then the numbers in the rows are merely in a different order, and so you are still adding the same four numbers. However, the shuffling changes the diagonals, and any other symmetric arrangements which can add up to the magic number, which exist in the above 4×4 pan-magic squares.

My take on the “Math Test” problem

Hits: 75

I have no desire to further pollute YouTube with yet another video on the “3 = ?” so-called “controversy”, so I seem to feel better trolling my own blog about it.

If you have a mindless social media account, like Instagram, Facebook, or Twitter, you would have seen this “Math Test” being spread around over the past few years:

From Vi Hart’s video (https://youtu.be/Twik7wqdwZU)

I don’t wish to pick on Victoria Hart, as she was the second person I have noticed embarking on this trend to make something profound out of this poorly-worded, poorly-conceived “Math Test”. This was similarly done by Hank Green of Vlog Brothers fame.

The person formulating the question didn’t seem to care enough about forming their question properly enough to articulate what they really meant. I don’t think I would be making any controversial statement to say that 7 and 56 are not equal, not even for large values of 7 ☺. In fact, none of the numbers are equal. The vloggers have attempted to  ascribe genius to the author of the question and attempt to look past the abused equal signs, but there is no consensus, meaning you can make any nonsense up that you like. The author of this “test” has never come forward, so we will never know what they meant by these tortured equations.

It means that the ambiguity in the question is just left for everyone else to waste their time arguing over. Hart and Green appear to be playing a part similar to interpreters of ancient manuscripts whose authors have vanished over thousands of years without a trace of their existence. Both engage in a kind of exegesis of the work with nothing really to go on, since there is no time or place for this “test”. But they have to find something to say because this “test” has gone so viral, and they have to score engagement points with their viewers and with YouTube.

While Hart and Green make heroic efforts at making such dodgy questions seem profound and serious (in my opionon, Vi Hart wins top honors here), their profundity carries too many assumptions about the author (whom no one has met, remember), to take anything seriously. Too many what-if’s. The only thing that comes out of both videos is that they have been thinking about this way too much. Hank Green tries to turn it into a statement on the state of humanity; while Vi Hart tries to use it to comment on how logic is usad to carry forward various social and cultural biases made by imaginary scholarly people who sit in their armchairs and rationalize all day, whom it is implied we should all hate.

If there was no internet, and this was written on a piece of paper, no one would think twice about dismissing it. It relies on a certain number sense, yet, also relies on a certain functional illiteracy toward math symbols so that people reading this question engage with it. It shouldn’t take an Einstein to see there is something wrong by saying that “9 = 90“. But there is also a number pattern. It would have been better to use function notation. If f(x) represented an unknown formula, I would feel better if it were written this way:

f(9) = 90

This first equation says that if you plug 9 into the formula, you get 90. Other numbers are thus plugged in to get numerical results that hopefully bear some kind of relationship:

f(8) = 72
f(7) = 56
f(6) = 42

Finally, you jump to f(3), and if you know the pattern, you know the answer. Or do you?

f(3) = y

I am not promoting f(x) as “the correct interpretation,” and I can’t be bothered to propose a solution that hasn’t already been seen countless times in discussions over many parts of the Internet. There have been passionate arguments over what f(3) is (12? 18?), because more than one pattern has been proposed. Because the question is so sketchy, and the writer of the question is AWOL, and can’t be held to account, there really isn’t going to be any consensus possible.

The question is easy to dismiss, since the writer of the question has given us much to be dismissive of. The real question to ask here is: is it a productive use of our time to engage in math questions where we are forced to make assumptions based on information we don’t have (and will likely never have)? I think everyone knows the answer to that.

Recreational Math I: Magic Squares: Matters of Primes: Part 7

Hits: 54

This article concerns the matter of prime numbers in writing computer programs that generate random magic squares of order n where n is prime. My article, like all the other articles in this series, is aimed at hobbyists.

Suppose you wanted to use the Reichmann (1957) algorithm to generate prime-ordered magic squares of any size n where n ≥ 5. If you asked a user to enter a prime number for the square dimensions, there comes a point where the user won’t exactly know, and shouldn’t be expected to know, if a large number is prime. Could an average person be expected to know, off the top of their head, that 127 is prime, for example? Or what about the number 349,871? Both are prime, but I cheated: I used software. But how does the software do it, since that is what you were trying to write?

We’ll get into the algorithm details in a bit. But the easiest thing to do is to have a detection if a number is not prime. If it isn’t prime, then you can’t use Reichmann’s algorithm, and you then need to ask the user for another (hopefully prime) number that is 5 or more. The user is making at best, quasi-educated guesses. Maybe he knows some powers of 2, and by subtracting 1, he could stumble on a Mersenne prime such as the number 127 mentioned earlier, or 31. Or 7.

Prime numbers are numbers divisible by 1 and itself, and has no other factors. A number like 5 is prime because only 1 and 5 divide it evenly. The same can be said of the number 2, since 1 and 2 are its only factors, making it the only even prime number. Numbers which have more than two factors are called composite numbers. A number like 12 has 1, 2, 3, 4, 6, and 12 as factors, and thus is not prime.

Mersenne primes refer to a special class of primes. They are primes of the general formula 2^p - 1, where the exponent p is also a positive prime number.

A programmer could start with a short list of known primes in an array. Let’s say: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, or the primes from 0 to 100. By the Fundamental Theorem of Arithmetic, a number n is composite if any prime number p \le \lfloor \sqrt{n}\rfloor divides it. The symbols \lfloor,  \rfloor represent the floor function which takes the square root of n and essentially chops off the decimal, returning only the integer part of the number. The array of known primes is thus good up to 100^2 = 10,000.

This is a shortcut, but if the user enters larger numbers than 10,000, you can try numbers beyond the scope of your array. But since the computer program doesn’t know any primes offhand beyond 97, the program could then attempt to check each whole number after 97, sqeuentially up to \lfloor \sqrt{n}\rfloor to see if anything divides n evenly. If none are found, then n is prime.

That’s a whole lot more efficient than checking each integer one by one up to n (or even \lfloor n/2 \rfloor, or half of n with the decimal chopped off) to see what divides n, if anything.

If no upper limit of n is specified to the user, even your more efficient \lfloor \sqrt{n}\rfloor could take hours or days to determine if n is prime, even on a computer considered to be fast. Also, for an n \times n magic square, it would be nice if there were only enough rows and columns to be reasonably displayed on a spreadsheet or console screen. On a spreadsheet, there is always the scrollbar to help you see the numbers that ran off the display. A 101 \times 101 magic square is possible for example, but requires a lot of scrolling to see the 10,201 cells containing the numbers. Today’s computers have lots of memory, so while it is possible to go far beyond even that, there is a matter of practicality. While it might be possible to check the correctness of a 324,773 \times 324,773 magic square programmatically, the 105.5 billion numbers it contains preclude the idea that it can be done on a normal household desktop computer running with 8 gigs of RAM.

Recreational Math I: Magic Squares: the “really good” kind – Part 6

Hits: 42

Welcome to part 6, where the magic squares are 7×7. 7 was the number of known planets in medieval times, as well as the number of known elements, and the number of days in the week. The rest is all alchemy.

The algorithm for a 7×7 matrix is the same as for 5×5. In fact, it should work for any odd prime-ordered matrix. Do the first based on a random sequence of the numbers between 1 and 7:

1 6 2 3 4 7 5
7 5 1 6 2 3 4
3 4 7 5 1 6 2
6 2 3 4 7 5 1
5 1 6 2 3 4 7
4 7 5 1 6 2 3
2 3 4 7 5 1 6

Now, do the second matrix using 0 and every seventh number after it up to 42:

7 42 0 21 14 35 28
0 21 14 35 28 7 42
14 35 28 7 42 0 21
28 7 42 0 21 14 35
42 0 21 14 35 28 7
21 14 35 28 7 42 0
35 28 7 42 0 21 14

The resulting matrix is magic, but not “hyper-magic” like the 5x5s were:

8 48 2 24 18 42 33
7 26 15 41 30 10 46
17 39 35 12 43 6 23
34 9 45 4 28 19 36
47 1 27 16 38 32 14
25 21 40 29 13 44 3
37 31 11 49 5 22 20

The matrix above was done using Excel, and checked using VBA, where I checked it for “magic”. The loss of hypermagic is unfortunate, but I suppose inevitable when you scale up. There are (7!)2 = 25,401,660 magic squares possible by this algorithm. The VBA proved handy later when I used dynamic programming to produce any odd-ordered magic square of any size, and investigated the results.

What if I made a mistake and got the shifting wrong, such as shifting from the second number from the left instead of the right? The result is variable; full magic can only be restored if the rightward diagonal is all 4/s:

7 3 6 2 5 1 4
3 6 2 5 1 4 7
6 2 5 1 4 7 3
2 5 1 4 7 3 6
5 1 4 7 3 6 2
1 4 7 3 6 2 5
4 7 3 6 2 5 1

Why does this work? This is because the sum of the numbers 1 to 7 in any order is 28. Notice that whatever number is in the opposite diagonal is repeated 7 times. There is only one number, when multiplied by 7, equals 28, and that is 4. All other numbers will result in a loss of magic. This appears to be a way to maintain magic in all odd-ordered magic squares past order 7. This can possibly generalize for squares of prime order n by taking the middle number (n+1)/2 and making it the main diagonal.

When added to the second preliminary square, both diagonals became magic, as well as the columns and rows. But because the opposite diagonal consists of all 4’s, we lose variety with this restriction, but not by much: 7!6! = 3,628,800. Still enough to occupy you on a rainy day.

9×9 magic squares won’t work so well, because the second square is shifted by 3, which is a number that divides 9. This will result in certain rows repeating in one of the pre-squares; and the result generally is that one of the diagonals won’t total the magic number (369 in this case).

13 66 78 59 27 8 34 38 46
74 55 22 3 33 41 54 17 70
26 7 29 37 49 12 69 77 63
32 45 53 16 65 73 58 21 6
48 15 68 81 62 25 2 28 40
64 76 57 24 5 36 44 52 11
61 20 1 31 39 51 14 72 80
9 35 43 47 10 67 75 60 23
42 50 18 71 79 56 19 4 30

This is a consequence of 9 not being a prime number. The algorithm that works so well for 5×5 and 7×7 magic squares breaks down when the odd number is composite. Thus, the algorithm is known to work for 11×11, 13×13, 17×17, and has been tried all the way to 101×101 and beyond on my Excel spreadsheet.

The magic number can be known in advance of any magic square because an order-n magic square obeys the formula (n × (n2 + 1))/2. The best bet is to look for odd-ordered n × n squares where n is prime. In my VBA program, I made double sure by sticking to magic squares where the order is prime. Here is an 11 × 11 square, where the magic number is 671:

112 20 60 87 3 41 92 33 105 51 67
62 78 2 42 93 32 102 52 70 121 17
11 39 95 23 101 53 71 120 14 63 81
96 26 110 50 73 111 13 64 82 10 36
109 47 74 114 22 61 84 1 35 97 27
75 115 21 58 85 4 44 94 29 100 46
12 57 86 5 43 91 30 103 55 72 117
83 7 34 90 31 104 54 69 118 15 66
37 99 28 106 45 68 119 16 65 80 8
25 107 48 77 116 18 56 79 9 38 98
49 76 113 19 59 88 6 40 89 24 108

Finally, to stretch things out to the boldly absurd, what about an order-23 square whose magic number is 6095?

92 369 279 275 108 466 226 403 125 327 360 316 55 457 485 14 192 182 234 425 36 145 524
283 271 115 461 210 413 131 328 364 311 56 442 498 17 193 181 232 428 31 159 510 80 381
103 473 214 409 138 323 348 321 62 443 502 12 194 166 245 431 32 158 508 83 376 297 257
228 395 126 335 352 317 69 438 486 22 200 167 249 426 33 143 521 86 377 296 255 106 468
129 330 366 303 57 450 490 18 207 162 233 436 39 144 525 81 378 281 268 109 469 227 393
365 301 60 445 504 4 195 174 237 432 46 139 509 91 384 282 272 104 470 212 406 132 331
63 446 503 2 198 169 251 418 34 151 513 87 391 277 256 114 476 213 410 127 332 350 314
488 15 201 170 250 416 37 146 527 73 379 289 260 110 483 208 394 137 338 351 318 58 447
196 171 235 429 40 147 526 71 382 284 274 96 471 220 398 133 345 346 302 68 453 489 19
236 433 35 148 511 84 385 285 273 94 474 215 412 119 333 358 306 64 460 484 3 206 177
45 154 512 88 380 286 258 107 477 216 411 117 336 353 320 50 448 496 7 202 184 231 417
507 72 390 292 259 111 472 217 396 130 339 354 319 48 451 491 21 188 172 243 421 41 161
386 299 254 95 482 223 397 134 334 355 304 61 454 492 20 186 175 238 435 27 149 519 76
266 99 478 230 392 118 344 361 305 65 449 493 5 199 178 239 434 25 152 514 90 372 287
464 218 404 122 340 368 300 49 459 499 6 203 173 240 419 38 155 515 89 370 290 261 113
399 136 326 356 312 53 455 506 1 187 183 246 420 42 150 516 74 383 293 262 112 462 221
324 359 307 67 441 494 13 191 179 253 415 26 160 522 75 387 288 263 97 475 224 400 135
308 66 439 497 8 205 165 241 427 30 156 529 70 371 298 269 98 479 219 401 120 337 362
452 500 9 204 163 244 422 44 142 517 82 375 294 276 93 463 229 407 121 341 357 309 51
10 189 176 247 423 43 140 520 77 389 280 264 105 467 225 414 116 325 367 315 52 456 495
180 242 424 28 153 523 78 388 278 267 100 481 211 402 128 329 363 322 47 440 505 16 190
430 29 157 518 79 373 291 270 101 480 209 405 123 343 349 310 59 444 501 23 185 164 252
141 528 85 374 295 265 102 465 222 408 124 342 347 313 54 458 487 11 197 168 248 437 24

That probably ran into the neighbouring columns, but it’s just for illustration. The code, which I have used to generate magic squares of as high an order as 113 (magic number 721,505) is fewer than 170 lines in VBA for Excel 2007. And yes, even the above square, and the order 113 square are magic.

Recreational Math I: Magic Squares: the “really good” kind – Part 5

Hits: 25

I have met with some disappointment as to how a methodology for creating a 4×4 square should pan out, and instead I have come up with many different algorithms, each resulting in its own small sets of magic squares, but had stumbled upon a set of squares with similar “hyper-magical” properties which I called the Durer Series.

I had met with some disappointment at this, and am still in the middle of writing my own 4×4 square program in Visual Basic .NET (it’s going to use a “brute force” algorithm … sorry!). With that, I had to learn about how .NET does objects. To those of you out of the loop on the recent .NET versions of VB, this language actually allows you to create your own novel objects, thus saving processing time if the right kind of objects are created. It’s a work in progress.

For now, I wanted to centre on the pièce de résistance of this series: that of the odd prime-ordered magic squares, those of order 5 and beyond. As I hinted at the beginning of this series, these are special and unique in that an algorithm can be made for an order-n magic square (where n is an odd prime greater than or equal to 5) which can generate (n!)2 squares, all magic (even after weeding out duplicates, the numbers of unique squares will still be in the thousands).

This seems to be a hard-to-find algorithm, except from a book called “The Fascination of Numbers”, written on the year of The Queen’s coronation in 1957 by W. J. Reichmann while he was still a headmaster at a Grammar School located in Spalding, Lincolnshire, England (according to my signed copy of the book, purchased from an English vendor through Amazon used books). I read it for the first time at a university library, and it is likely to be found in a similar univeristy or college collection near you.

What I like about this algorithm is that while I have used it to write computer programs for magic squares, it really requires no more than pencil and paper, and a bit of skill at addition.

First of all, write down the numbers from 1 to 5, in any order at all:

3, 2, 1, 5, 4

A 5×5 square matrix is constructed in a pattern by starting from the fourth number in the sequence, then proceeding with the 5th number, then the first, second and finally the third:

4 2 1 5 3
5 3 4 2 1
2 1 5 3 4
3 4 2 1 5
1 5 3 4 2

Next, scramble the first 5 multiples of 5 (counting 0):

20, 0, 5, 15, 10

Create a 5×5 matrix by shifting the order of these numbers by 2 to the left as follows:

20  0  5 15 10
 5 15 10 20  0
10 20  0  5 15
 0  5 15 10 20
15 10 20  0  5

Now add them together, adding together numbers located in the same columns and rows in both squares, as in matrix addition:

24262013
101814221
12215819
39171125
16152347

The result is a magic square which has many more ways of obtaining  the magic number 65 than just adding the rows, columns, and diagonals. Taking any 3×3 sub-square on the corners or left/right sides of this square and adding its corner numbers to the middle number will obtain 65. This seems to be true of all magic squares made by this method. Since both squares can be scrambled independently, there will be (5!)2 = 14,400 possible squares before duplicates are weeded out. I have written programs in many languages regarding this 5×5 square, and can attest to the robustness of this algorithm in producing a nearly endless series of 5×5 magic squares, all sharing very similar “magical” properties.

Reichmann also reminds us that the scrambled 5×5 squares we created initially are also magic.

I suspect there may be more than 14,400 magic squares. Are there any squares that cannot be produced by this algorithm? This must be checked against a “brute-force” (read: computational) method for generating all possible magic squares to see if this is really the definitive number. It is certainly a couple of orders of magnitude greater than what Danny Dawson did with his 4×4 squares (around 920 squares or so, with some duplicates). It would also be interesting to know what squares could not have originiated from Reichmann’s algorithm (proven by working backwards to show, supposedly, that duplicate numbers appear in some rows of the first or second matrix, which disappears in the resultant matrix.

It also seems that 4×4 and 5×5 squares can have such “compound” magical properties; it is harder to find for 7×7 matrices, although they are additive to the magic number in just enough ways so as to say they are magic. We’ll leave that for the next journal entry.

Recreational Math I: Magic Squares: the “really good” kind – Part 4

Hits: 137

How to Make a Random Square

I have noticed that it has been difficult to elucidate a method for systematically creating even-ordered magic squares of any but the most basic kind. I don’t know why this is, since the art has been alive in Europe for at least 600 years, and probably longer in other cultures. There are a few methodologies out there, but they are perfunctory. Such as, this method offered by Reichmann (1957), which tells us little more than to write the numbers down from 1 to 16 and reverse the order of the diagonals:

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

Well, only a limited number of magic squares are possible that way. You can reverse the order of the numbers, or write the numbers in order down the columns instead of the rows, followed by a similar reversal of the diagonals (these would not be considered to be unique solutions, since these methods amount to mirroring or rotating the square).

After some playing around with known 4×4 magic squares (thanks to the thorough resource offered here), and using some simple rules, I think I may have come upon a method on my own. I would suppose that it could not generate all magic squares possible, but I don’t intend to offer a complete solution. I estimate that I would be able to generate maybe another 10 or so magic squares with this method. The intent here is to allow the reader to create a 4×4 magic square sitting down at a table with little more than pencil and paper. I am sure this methodology is published elsewhere, but I wasn’t able to come up with anything.

I just have a few simple rules in coming up with the method. First of all, all of the 16 numbers are the result of a sum of two smaller numbers a and b, where a is a number from 1 to 4 such that a = {1, 2, 3, 4}, and b is a multiple of 4: b = {0, 4, 8, 12}. All 16 unique numbers generated will be the result of adding a number from set a to a number in set b.

The numbers 1, 2, 3 and 4 are the result of adding set a to 0 in set b.

The numbers 5, 6, 7, 8 come from adding set a to 4 in set b.

The numbers 9, 10, 11, 12 come from adding set a to 8 in set b.

And the numbers 13, 14, 15, 16 come from adding set a to 12 in set b.

My solution is inspired by Reichmann’s (1957) work on odd-ordered matrices. This solution on even-ordered matrices is quite different than for odd-ordered matrices, but the idea of adding two matrices and getting a magic square appealed to me, so I went with that basic idea.

The first matrix uses numbers from set a, in the following pattern:

4  3  2  1
1  2  3  4
1  2  3  4
4  3  2  1

These numbers may be randomized, but I’ll just stick to numeric order for now. The second matrix is composed of the numbers of set b arranged in a pattern reminiscent of the Durer square:

12  0  0 12
 4  8  8  4
 8  4  4  8
 0 12 12  0

The sum of the two matrices has all the magic of the Durer square, and consist of the unique numbers 1 to 16:

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

In fact, it is the Durer square itself. Now that we know it works, what about mixing things up? Take note of the patterns. Let the first matrix represent numbers {a, b, c, and d} belonging to set a; and {e, f, g, and h} belonging to set b. whatever randomization is chosen, the numbers must fit the following scheme:

a  b  c  d      e  h  h  e
d  c  b  a      f  g  g  f
d  c  b  a   +  g  f  f  g
a  b  c  d      h  e  e  h

a, b, c, and d may be any permutation of the numbers 1 to 4, without apparent limitation. The second and third rows on teh first matrix are the reversals of the first and fourth. The second matrix, however is much more restricted. Considering e and f to be one pair; g and h to be another pair: 12 can only be paired on the same row with 0; and 4 only be paired with 8. Otherwise, no joy.

So, on with a randomized square: let a = {3, 1, 4, 2} and b = {8, 0, 12, 4}

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

What about a = {2, 4, 1, 3}, and b = {4, 12, 0, 8}

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

You may also treat sets a and b oppositely, while randomizing. This time the restriction is on matrix a — 4 pairs with 1, and 2 pairs with 3:

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

The reason I say these squares are limited to about 10 or so, is because as you can see, the individual rows or columns end up being about the same — at best, a shuffling or reversal the same rows found in the Durer square. There are at best 24 such squares, not subtracting tilted (squares the same when laid on its side) or reflected squares (where the squares are mirror images of each other). All that might be said, is that we may have discovered all the ways a magic square may be constructed with the same “hyper-magic” as the Durer square, which may be a pretty cool thing to say actually. It’s just that my goal was more ambitious at the start. I will refer to the set of 4×4 hypermagic squares built on the same rules as “the Durer series.”

Any known magic square, however, is the result of a unique combination of the basic squares built on sets a and b. Respectively, I will call the matrices A and B.  This allows us to work backwards from a 4×4 square we know is magic, and discern any patterns. The above squares were done that way. What patterns can be observed for the following square, which this website offers as #379 out of 900 or so others?

                Matrix "A"      Matrix "B"
13 12  6  3     1  4  2  3     12  8  4  0
10  5 11  8     2  1  3  4      8  4  8  4
 7 16  2  9  =  3  4  2  1  +   4 12  0  8
 4  1 15 14     4  1  3  2      0  0 12 12

One may discern a pattern here, but it is not all that obvious, especially for the square built on set b. It doesn’t seem to resolve itself into simple rules and patterns the way the Durer series of squares did. Also, you might have noticed that this square is not quite as “magic” as the Durer series, though all rows, columns, and both diagonals give you 34.

But I do notice that in matrix A the last column is a reversal of pairs of the first column. Also, the third column is built on the middle numbers of the first column, and the second column are built on the middle numbers of the last column.

What of Matrix B? The first and last columns are reversals (of all 4 numbers, not of pairs), the second and third both start with the middle two numbers, then first, then last from their adjacent end columns: “e f g h” becomes: “f g e h”.

When I tried to build on these rules, I got duplicate numbers in my squares (and with that, some missing numbers). It would appear that the observable number patterns in such squares are limited to getting you maybe 4 to 8 unique squares, then you have to make a new set of rules each time. With over 900 squares discovered (give or take a duplicate or two), that’s a lot of rules. The total number of 4×4 squares, both magic and not, are equal to 16! = 21 trillion possible squares. Finding all possible magic squares, weeding out any duplicates, reflections, and sideways duplicates would seem a tad non-trivial. Even at the website I referred to for these squares, it has been shown that the number “924” claimed as the number of possible 4×4 magic squares, might be too high, due to some duplicates found. Their scripts took just under 25 seconds to generate and check these squares — but just to check them for magic, less so for duplication, which sounds like it would take more computer time.

Recreational Math I: Magic Squares: the “really good” kind – Part 3

Hits: 100

Notice that to show the rules for making these kind of magic squares, I used only odd-ordered square matrices as examples. What about matrices of even numbers of rows and columns? The rules for these vary.

This is a small part of a 1514 engraving by Albrecht Durer, called Melancholia. The author of the article that houses this graphic asserts that there are 32 possible 4×4 magic squares with the famous pun “1514” in the same position as above. This magic square has a symmetry in the numbers, as explained below.

The famous Durer magic square, with the year of the engraving cleverly made a part of a magic square, has a certain organization in its construction, as well as a certain symmetry. The numbers are constructed, in sequence:

_   3   2   _        _   3   2   _
_   _   _   _        5   _   _   8
_   _   _   _  ====> _   6   7   _
4   _   _   1        4   _   _   1         

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

So, you start from the bottom right and proceed in a horseshoe to the top then the bottom left. The next diagram places the numbers 5-8 in a pattern that is left-to-right u-shape. Then the same u-shape for the numbers 9-12 from left to right, except this time it’s upside-down. Finally ending as we started, the same horseshoe shape (except right side up) from right to left.

Durer’s square has many things about it, apart from its magic number (34) which works on all the attendant diagonals, rows and columns. The middle 4 squares add up to 34 (10 + 11 + 6 + 7 = 34); the four corners add to 34 (16 + 13 + 4 + 1 = 34), and all corner foursomes add to 34: (16 + 3 + 5 + 10); (2 + 13 + 11 + 8); (9 + 6 + 4 + 15); and (7 + 12 + 14 + 1).

The numbers at the ends of the two middle rows add to 34:

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

and the numbers at the tops and bottoms of the two middle columns add to 34:

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

If we take another symmetrical combination: a rightward-slanting rectangle whose corners are 2, 8, 9, and 15, these also add to 34:

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

The leftward-slanting rectangle, whose corners are 5, 3, 12, and 14 also add to 34:

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

Starting from 2 and proceeding in an “L”-shape to the left to the number 5, and continuing counter-clockwise in the same manner gets us the corners of a tilted square whose numbers 2, 5, 15, and 12, add to 34:

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

Starting from “3” and doing likewise yields the numbers 3, 9, 14, and 8, also adding to 34:

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

And what about this talk about “symmetry”? By this, we mean that we may take pairs of numbers at the start and end of any row, and they add up to the same number in a symmetrical place elsewhere. 16 + 3 = 4 + 15, taking the top and bottom of the first and second column. Likewise can be done for the last two columns: 2 + 13 = 14 + 1. The middle two rows have the same property: 5 + 10 = 9 + 6; and 11 + 8 = 7 + 12. On a larger scale, the sums of the middle two rows of columns 1 and 2 are the same as the tops and bottoms of columns 3 and 4: 11 + 8 = 7 + 12 = 16 + 3 = 4 + 15. Likewise, the sums of the middle two rows of columns 3 and 4 are the same as the tops and bottoms of columns 1 and 2: 2 + 13 = 14 + 1 = 5 + 10 = 9 + 6. These two groups of symmetrical numbers are illustrated below in red and green:

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

The sums of 15 (green) and 18 (red) across each row form this pattern

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

The downward symmetry is also interesting. Here, the sum of 25 is in gold and the sum of 9 is in blue. In the process, we can discern the patterns that we used to construct the square in the first place:

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

This is an incredible amount of magic, but if you follow the order of filling (horseshoes are right-to-left, u-shapes are left-to-right, along with the peculiar pattern of filling u’s and horseshoes), there really are four possible patterns that have these “hyper-magic” qualities, but you lose the “1514” idea in two of them:

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

Of course, you could reverse all of the numbers in the rows of the first two squares to get your “1514” back.

Every time I look at that darned Durer square, I keep seeing more patterns. I think there comes a point where one has to leave the remaining observations up to the reader.

There is yet another 4×4 square, and with it we can increase the magic, if that can even be conceivable after all I have said. But there is a square with even more magic than the Durer square. R. J. Reichmann mentioned it in his book “The Fascination of Numbers”, first published in 1957. The square could be constructed like this:

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

This square has all the magic of the Durer square and then some. One thing this new square has over the Durer square is that any four numbers in square formation will add to 34, from anywhere in the square. These include the foursomes:

10  3     16   9     11   2       4   5
5  16      2   7      8  13      14  11

Recreational Math I: Magic Squares: the “really good” kind – Part 2

Hits: 29

Last time I introduced the idea of magic squares. I promised I would show you how to make one. In this post, I will begin by discussing “trivial” squares, or squares made by simple rules of following diagonals and wrapping.

When I say a square is “magic”, I mean that all rows, columns, and diagonals add up to the same number. While other sources, such as Wolfram’s Mathematica, say that only the main diagonal of the matrix need be magic, I will take the more strict requirement that both leftward and rightward diagonals have to be magic.

There are trivial magic squares that begins by following a rule where you start with “1” in the top middle square, then move up and to the right one square, and place a “2” there.

_  1  _
_  _  _
_  _  _

But you may have noticed that if you start at the top, how can you move “up and to the right”? You get around this by “wrapping” to the bottom, treating the bottom of the rightward column as though it is above.

_  1  _
_  _  _
_  _  2

OK, you say, but now there’s no “right” after the last column. Now what? Now you can wrap so that the leftmost column is treated as “right of” the rightmost column:

_  1  _
3  _  _
_  _  2

Now another problem: up and to the right of “3”, there is a “1” in the way. If this happens, you are allowed to place the fourth number below the “3”:

_  1  _
3  _  _
4  _  2

Now, following these rules and exceptions, we can keep going:

8  1  6
3  5  7
4  9  2

The result is a magic square whose rows, columns and diagonals add up to 15.

I found that if I moved the 1 elsewhere and followed these rules in the same manner, some or all of the “magic” is lost. There seems to be only one magic square that can be made using these rules, at least one that adds up to 15 in all of its rows, columns and diagonals. The following 3×3 magic squares were the closest I could come to any credible “magic” by placing the “1” in a different position:

6  8  1                4  9  2
7  3  5 and similarly: 8  1  6
2  4  9                3  5  7

But notice in both cases, neither of the diagonals add up to 15. In the next post, I will discuss a way to break this limitation, making it possible to construct up to 36 3×3 magic squares.

Meanwhile, let’s expand the idea to 5×5, using the same, identical rules. This one seems easier in a way, since there aren’t as many blockages early on:

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

I particularly like 5×5 squares. But my experience with placing the “1” elsewhere than the exact middle position of the top row has resulted in a loss of “magic”. However, I was lucky on my first attempt with moving the “1” around. The following magic square has a “Mathematica” level of magic:

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

Later in this series, we can break this limitation, too. But next, we shall discuss some 4×4 magic squares, including one that made history.

Recreational Math I: Magic Squares: the “really good” kind

Hits: 31

Introduction

ONE OF THE few things you see on the web these days is how to do a really good magic square. There are many websites that tell you about how spiralling arrangements of sequential numbers on a square matrix is magic, but for me, that’s dull. You are limited to doing seemingly less than a dozen such magic squares, so I don’t find them too interesting.

Recall that magic squares are numbers arranged in a square matrix such that each of its rows and columns, and normally both diagonals add up to the same number. Usually, a square of n numbers to a side which has numbers total, will be populated with the entire set of numbers from 1 to n inclusive, in some quasi-random order. These numbers would be arranged in such a manner that the total of each of its rows, columns, and both diagonals equal the same “magic number”, which is different depending on the dimensions of the square. By using random methods suggested in this article, the number of magic squares possible, when n is odd, is equal to (n!)2.

For the 5×5 square, you apparently have to start by moving from the current position to the “top right” square (wrapping to the opposite edge if necessary), and if that square is occupied, move down by 1 square. This non-random, deterministic method apparently works for all squares greater than 5×5 (with odd dimensions).

I read from an old book on recreational math (The Fascination of Numbers, by W. J. Reichmann (1958)), that

  1. Squares of even dimensions (4×4, 6×6) have to be arranged by a different algorithm than squares of odd prime dimension (5×5, 7×7, 11×11, …).
  2. A randomly-generated 5×5 magic square can be made which uses the sum of two matrices.

The number of possible permutations of 5×5 matrices is equal to (5!)2.

Reichmann’s book was the only place where I could find such an algorithm. This seems to be a rare algorithm, even on an internet search. But it is the only method that leads to “magic” results in a variety of ways. These squares seem to be the most robust in terms of the number of ways their “magic” qualities can be determined. They have inspired my writing computer programs that generate such squares as a way of practicing programming several years ago. I have written magic square programs following Reichmann’s algorithm (not sure if he originated it) in VB5, Visual Basic .NET, VB for applications (in Excel), and in Microsoft Quick Basic 4.5. The 16-bit QB 4.5 version does not run on my 64-bit machine, and for similar reasons, neither does the VB5 version, whose runtime DLL is no longer supported by later versions of MS Windows.

In the next instalments, starting this coming Saturday, I will begin to discuss the making of 3×3 and 5×5 squares, and discuss their magic properties.