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

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:

1623475
7516234
3475162
6234751
5162347
4751623
2347516

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

742021143528
021143528742
143528742021
287420211435
420211435287
211435287420
352874202114

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

848224184233
7261541301046
1739351243623
349454281936
4712716383214
2521402913443
3731114952220

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:

7362514
3625147
6251473
2514736
5147362
1473625
4736251

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).

13667859278343846
74552233341541770
26729374912697763
32455316657358216
48156881622522840
64765724536445211
61201313951147280
93543471067756023
42501871795619430

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:

11220608734192331055167
62782429332102527012117
113995231015371120146381
962611050731111364821036
10947741142261841359727
75115215885444942910046
12578654391301035572117
83734903110454691181566
37992810645681191665808
25107487711618567993898
49761131959886408924108

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

92369279275108466226403125327360316554574851419218223442536145524
28327111546121041313132836431156442498171931812324283115951080381
10347321440913832334832162443502121941662454313215850883376297257
22839512633535231769438486222001672494263314352186377296255106468
12933036630357450490182071622334363914452581378281268109469227393
3653016044550441951742374324613950991384282272104470212406132331
6344650321981692514183415151387391277256114476213410127332350314
48815201170250416371465277337928926011048320839413733835131858447
1961712354294014752671382284274964712203981333453463026845348919
236433351485118438528527394474215412119333358306644604843206177
4515451288380286258107477216411117336353320504484967202184231417
50772390292259111472217396130339354319484514912118817224342141161
3862992549548222339713433435530461454492201861752384352714951976
266994782303921183443613056544949351991782394342515251490372287
4642184041223403683004945949962031732404193815551589370290261113
3991363263563125345550611871832464204215051674383293262112462221
3243593076744149413191179253415261605227538728826397475224400135
308664394978205165241427301565297037129826998479219401120337362
452500920416324442244142517823752942769346322940712134135730951
10189176247423431405207738928026410546722541411632536731552456495
18024242428153523783882782671004812114021283293633224744050516190
43029157518793732912701014802094051233433493105944450123185164252
14152885374295265102465222408124342347313544584871119716824843724
The 23 columns may not all be visible here.

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.

Leave a Reply

%d bloggers like this: