Constructing Magic Squares of nth Powers

by François Labelle

This page describes the methods I used to construct:

Contents

Definitions

Background

In 2006, Lee Morgenstern found methods to construct

To me these are not individual results, but rather special cases of a more general method to construct a (rs)×(rs) semi-magic square by using a taxicab(n,r,s) and a taxicab(n,s,r). My first goal is to explain the generalization, which I call the taxicab method.

The taxicab method (semi-magic version)

Choose a taxicab(n,r,s)

xa=a11n + a12n + ... + a1rn
=a21n + a22n + ... + a2rn
=...
=as1n + as2n + ... + asrn

and a taxicab(n,s,r)

xb=b11n + b12n + ... + b1sn
=b21n + b22n + ... + b2sn
=...
=br1n + br2n + ... + brsn

such that all possible r2s2 products aijbuv are distinct.

Let P1, ..., Ps be s Latin squares of size r × r over the elements {1,2,...,r}.

Let Q1, ..., Qr be r Latin squares of size s × s over the elements {1,2,...,s}.

Construct a (rs)×(rs) square M consisting of r × s blocks Mij each of size s × r

M11M12...M1s
M21M22...M2s
............
Mr1Mr2...Mrs

where element (u,v) of block Mij is given by

(Mij)uv = (a[j, Pj[i,v]] b[i, Qi[j,u]])n (using some brackets instead of subscripts for clarity).

Then M is a (rs)×(rs) semi-magic square of nth powers, with magic sum xaxb.

Proof.
To compute the sum of every element in a row, we fix i and u, and sum over j and v:

Σj Σv (Mij)uv=Σj Σv (a[j, Pj[i,v]] b[i, Qi[j,u]])n(by definition)
=Σj b[i, Qi[j,u]]n Σv a[j, Pj[i,v]]n(since the b[] term doesn't depend on v)
=Σj b[i, Qi[j,u]]n Σv a[j,v]n(since Pj[i,v] is just a permutation of v)
=xa Σj b[i, Qi[j,u]]n(by the taxicab property for a)
=xa Σj b[i,j]n(since Qi[j,u] is just a permutation of j)
=xaxb(by the taxicab property for b).

So every row of the square M sums to xaxb. The proof that every column sums to xaxb is similar. End of proof.

Note that the sr elements in block Mij come from products of a[j,·]n (r numbers from taxicab a) and b[i,·]n (s numbers from taxicab b). The specific arrangement of those sr numbers is determined by the Latin squares.

Some taxicabs

Below are links to The On-Line Encyclopedia of Integer Sequences corresponding to some generalized taxicabs. The sequences only give the taxicab sums, not how to realize the sums, but you can find that information in at least one reference when the link is highlighted.

  in 2 ways in 3 ways in 4 ways in 5 ways
sum of 2 squares taxicab(2,2,2) taxicab(2,2,3) taxicab(2,2,4) taxicab(2,2,5)
sum of 3 squares taxicab(2,3,2) taxicab(2,3,3) taxicab(2,3,4) taxicab(2,3,5)
sum of 4 squares taxicab(2,4,2) taxicab(2,4,3) taxicab(2,4,4) taxicab(2,4,5)
sum of 2 cubes taxicab(3,2,2) taxicab(3,2,3) taxicab(3,2,4) taxicab(3,2,5)
sum of 3 cubes taxicab(3,3,2) taxicab(3,3,3)    
sum of 4 cubes taxicab(3,4,2) taxicab(3,4,3)    
sum of 2 4th powers taxicab(4,2,2)      

Note that a taxicab like 50 = 12 + 72 = 52 + 52 can't be used to create a semi-magic square because its components aren't distinct. We can also discard a taxicab when the gcd of its components isn't 1 because the gcd will divide the semi-magic square entries.

A 12×12 semi-magic square of 4th powers

Let n = 4, r = 4, and s = 3.

I choose the taxicab(4,4,3)

650658=24 + 164 + 214 + 254
=54 + 114 + 124 + 284
=134 + 194 + 204 + 244

and the taxicab(4,3,4)

53809938=24 + 714 + 734
=174 + 624 + 794
=294 + 534 + 824
=374 + 464 + 834.

One can verify with a computer that all 144 products aijbuv are distinct.

Let P1 = P2 = P3 =

1234
2341
3412
4123

Let Q1 = Q2 = Q3 = Q4 =

123
231
312

Applying the method gives

(2·2)4(16·2)4(21·2)4(25·2)4
(2·71)4(16·71)4(21·71)4(25·71)4
(2·73)4(16·73)4(21·73)4(25·73)4
(5·71)4(11·71)4(12·71)4(28·71)4
(5·73)4(11·73)4(12·73)4(28·73)4
(5·2)4(11·2)4(12·2)4(28·2)4
(13·73)4(19·73)4(20·73)4(24·73)4
(13·2)4(19·2)4(20·2)4(24·2)4
(13·71)4(19·71)4(20·71)4(24·71)4
(16·17)4(21·17)4(25·17)4(2·17)4
(16·62)4(21·62)4(25·62)4(2·62)4
(16·79)4(21·79)4(25·79)4(2·79)4
(11·62)4(12·62)4(28·62)4(5·62)4
(11·79)4(12·79)4(28·79)4(5·79)4
(11·17)4(12·17)4(28·17)4(5·17)4
(19·79)4(20·79)4(24·79)4(13·79)4
(19·17)4(20·17)4(24·17)4(13·17)4
(19·62)4(20·62)4(24·62)4(13·62)4
(21·29)4(25·29)4(2·29)4(16·29)4
(21·53)4(25·53)4(2·53)4(16·53)4
(21·82)4(25·82)4(2·82)4(16·82)4
(12·53)4(28·53)4(5·53)4(11·53)4
(12·82)4(28·82)4(5·82)4(11·82)4
(12·29)4(28·29)4(5·29)4(11·29)4
(20·82)4(24·82)4(13·82)4(19·82)4
(20·29)4(24·29)4(13·29)4(19·29)4
(20·53)4(24·53)4(13·53)4(19·53)4
(25·37)4(2·37)4(16·37)4(21·37)4
(25·46)4(2·46)4(16·46)4(21·46)4
(25·83)4(2·83)4(16·83)4(21·83)4
(28·46)4(5·46)4(11·46)4(12·46)4
(28·83)4(5·83)4(11·83)4(12·83)4
(28·37)4(5·37)4(11·37)4(12·37)4
(24·83)4(13·83)4(19·83)4(20·83)4
(24·37)4(13·37)4(19·37)4(20·37)4
(24·46)4(13·46)4(19·46)4(20·46)4

which gives the 12×12 semi-magic square

44324424504355478148524198849494138741460417524
142411364149141775436548034876420444264384404484
14641168415334182541042242445649234134941420417044
2724357442543446824744417364310415014158041896410274
992413024155041244869494842212439543234340440842214
12644165941975415841874204447648541178412404148848064
6094725458446446364148442654583416404196841066415584
111341325410648484984422964410490245804696437745514
172242050416441312434848124145431941060412724689410074
9254744592477741288423045064552419924107941577416604
1150492473649664232444154913499648884481470347404
20754166413284174341036418544074444411044598487449204

The magic sum is 650658 · 53809938 = 35011866639204. The largest entry is 284·834 = 23244. This is the smallest possible magic sum and smallest possible largest entry using compatible taxicab(4,4,3) and taxicab(4,3,4). The diagonal sums are 12005752179109 and 45297006668644 which are not magic.

New semi-magic squares from other people

After I published my taxicab method formula on this webpage, Christian Boyer created:

Obtaining magic diagonals given a semi-magic square

A random model

Semi-magic squares produced by the taxicab method are unlikely to be fully magic. For a k × k semi-magic square with magic sum m, the entries can be thought as random with mean m/k and standard deviation c·m/k where in practice c is close to 1. For example, c ≈ 0.577 if the entries are the numbers 1 through k2, and c ≈ 1.867 for the 12×12 semi-magic square above. Under this random model, diagonal sums have mean m and standard deviation c·m/√k. Assuming a normal distribution, the probability that a given diagonal sum equals m is √k/(√c·m). Typically this expression is dominated by m, so for simplicity I will say that the probability is ~1/m. To get two correct diagonal sums, we need to square this probability, which gives

pmagic = k/(2πc2m2),

or approximately 1/m2. In the 12×12 semi-magic square above, pmagic ≈ 4.5×10-28.

Modular correction

The random model above requires a correction because magic square entries are not always uniformly distributed modulo prime powers, so a sum of k entries is not necessarily uniformly distributed modulo prime powers either. The effect can be surprisingly important. I define the correction factor modulo M to be M times the probability of obtaining the magic sum m as a sum of k random entries modulo M.

As an example, the table below gives correction factors modulo 5 assuming a square with entries chosen from the 12×12 semi-magic square above. The actual square has k=12 and m≡4 (mod 5), so its correction factor modulo 5 is 1.30.

magic sum modulo 501234
correction for k=1 1.253.750.000.000.00
correction for k=2 0.311.882.810.000.00
correction for k=3 0.080.702.112.110.00
... ...............
correction for k=12 1.220.830.670.971.30

The complete correction factor takes into accounts all prime powers, and must be squared because we're matching 2 diagonals. It can be defined as

fmod = Πp prime (correction factor when expressing m as a sum of k entries modulo pe)2.

where e is the largest exponent such that pe ≤ 1024 (a bound to make sure that the effect is local). The modular correction tells how much more likely it is to get 2 diagonals summing to m because of modular considerations. For the 12×12 semi-magic square above, fmod ≈ 25.2, primarily due to effects modulo 5 and modulo 16.

Permuting rows and columns

Given a semi-magic square, we can generate more semi-magic squares by permuting its rows and columns any way we like. This gives us more than one chance of getting correct diagonals. There are (k!)2 ways of permuting the rows and columns of a k × k square. Because of symmetries, this really gives only

nperm = (k!)2 / ([k/2]! 2[k/2]+1)

independent chances of success (for k ≥ 2), where [] denotes the floor function. For k=12, this is 4.98×1012.

Combining the last three formulas, the expected number of magic diagonal pairs that can be obtained (counting equivalent cases only once) is

pmagic · fmod · nperm = fmod k(k!)2 / (2πc2m2 [k/2]! 2[k/2]+1)

where

An O(m2)-time algorithm

If nperm > m2, then a solution is likely to exist. We can try random row and column permutations. After ~m2 attempts we should find a solution.

An O(m)-time algorithm

Note that if we apply the same permutation to both rows and columns, then elements from the main diagonal stay in the main diagonal, and so their sum stays unchanged. This suggests the following algorithm:

  1. Apply random permutations of rows (or columns) until we get a correct main diagonal. This should take ~m attempts.
  2. Generate a random permutation and apply it to both rows and columns until we get a correct anti-diagonal. This should take ~m attempts.

Mathematically, this is splitting nperm as k!/2 × k!/([k/2]! 2[k/2]). We need k!/([k/2]! 2[k/2]) > m for a solution to step 2 to be likely to exist.

This is apparently the method that was used by Hugo Pfoertner to find magic diagonals in a 44×44 semi-magic square of 4th powers in October 2007. In his case, m=123784061778806 and k=44, so 44!/(22! 222) ≈ 5.64×1026 which is plenty large for the algorithm to work.

An O(m)-time, O(√m)-space algorithm

If we have plenty of permutations available, then we can add constraints to the permutations and still be ok. The trick is to split the indices {1,2,...,k} into two sets S and T. Generate ~√m permutations S→S and store them keyed by the sum of diagonal entries restricted to S. Similarly, generate ~√m permutations T→T and store them keyed by m - (sum of diagonal entries restricted to T). Look for a collision between the two sets to obtain a diagonal with sum m.

Doing the anti-diagonal is tricky as each set S and T should be symmetric (x is in S iff k+1-x is in S). One can choose S = {1,2,...,q} union {k+1-q, ..., k-1, k} where q ≈ k/4, and let T be the remaining middle part. The condition for this algorithm to work is roughly (k/2)!2 / ((k/4)!22k/2) > m.

Obtaining magic diagonals given compatible taxicabs

In the previous section I discussed turning a given semi-magic square into a fully magic square by permuting rows and columns. But if the semi-magic square comes from the taxicab method, there is even more flexibility.

Rearranging the taxicabs

Note that in the taxicab(n,r,s)

xa=a11n + a12n + ... + a1rn
=a21n + a22n + ... + a2rn
=...
=as1n + as2n + ... + asrn

we can

It turns out that permuting the rows of the taxicabs xa and xb will permute the blocks Mij, which doesn't give us anything new since we're already planning to permute the rows and columns of M individually. Permuting the elements within each row of the taxicabs does give us something new, but the same permutation can be accomplished by choosing Latin squares, so in the end there is no need to rearrange the taxicabs.

Choosing Latin squares

We can assume that the first row of each Latin square P1, ..., Ps, Q1, ..., Qr is ordered, because any other ordering can be obtained later by permuting the rows and columns of M. We do not assume that the first column of each Latin square is ordered because a similar claim doesn't hold for columns. We're interested in the number of such Latin squares, which is given by the integer sequence A000479.

Order of the
Latin square
Number of Latin squares
with sorted first row
11
21
32
424
51344
61128960

This gives us

nlatin = A000479(r)sA000479(s)r

possible combinations. For a given compatible taxicab pair taxicab(n,r,s) and taxicab(n,s,r), the expected number of possible magic diagonal pairs becomes

pmagic · fmod · nperm · nlatin = fmod A000479(r)sA000479(s)r rs((rs)!)2 / (2πc2m2 [rs/2]! 2[rs/2]+1).

Search algorithms

The basic algorithm is:

  • For every combination of Latin squares:
    • Search the corresponding semi-magic square for two diagonals.

This works fine for small squares, but for large squares it is too expensive to do O(√m) work for each combination of Latin squares. The trick is to do some precomputation in order to speed up the inner loop. The revised algorithm is:

  • Construct a list of sets of k entries with a magic sum.
  • For every combination of Latin squares:
    • For every set in our list:
      • Mark the set if its entries fall on different rows and columns.
    • If two of the marked sets obey a compatibility condition:
      • Success!

A 16×16 magic square of 4th powers

Let n = 4, r = 4, and s = 4.

I choose the two taxicabs(4,4,4)

1950354=24 + 214 + 294 + 324
=74 + 234 + 244 + 344
=84 + 94 + 164 + 374
=144 + 264 + 274 + 314

and

321793923=34 + 854 + 974 + 1164
=234 + 254 + 984 + 1234
=434 + 814 + 954 + 1184
=454 + 734 + 1064 + 1134.

One can verify with a computer that all 256 products aijbuv are distinct. As we know by now, this gives a 16×16 semi-magic square with magic sum m = 1950354 · 321793923 = 627612064898742. The largest entry is 374·1234 = 45514. This is the smallest possible magic sum and smallest possible largest entry using two compatible taxicabs(4,4,4).

Using the formulas above, the expected number of successes if we search all possible permutations is pmagic · fmod · nperm ≈ 6.1×10-10, which doesn't look promising. Using the Latin squares, we get a factor of 248 boost. The expected number of successes becomes pmagic · fmod · nperm · nlatin ≈ 67, which is enough.

The numbers were tight and I could not afford to add constraints on the permutations.

The first step of the algorithm was to search for sets of 16 entries such that

  1. their sum equals the magic sum, and
  2. their positions can potentially form a diagonal after rearrangement.

To explain what I mean by (2), recall from the taxicab method (semi-magic version) that the 256 entries are arranged in 16 blocks of 16 numbers, and that the Latin squares shuffle numbers within blocks and never swap numbers across blocks. Consider a diagonal in a 16×16 square. Note that if we permute the rows and columns of the square, then the diagonal entries get shuffled, but we still get 1 diagonal entry per row, and 1 diagonal entry per column. Now if we further shuffle the diagonal entries using Latin squares, then those properties don't necessarily hold, but what remains true is that the top 4 rows together contain 4 diagonal entries, and similarly for the next groups of 4 rows, and same for columns.

To illustrate this, the table below gives the number of diagonal entries that fall into each block of 4×4 entries for some arbitrary permitted shuffling. Those counts can be anything from 0 to 4, but note that the sums across rows and columns are always 4.

 cols 1-4cols 5-8cols 9-12cols 13-16sum
rows 1-401214
rows 5-821104
rows 9-1221014
rows 13-1601124
sum444416

It turns out that among all Choose(256, 16) possible ways to choose a diagonal's entries, only about 0.0257% of the choices satisfy those sum conditions. I searched for those only, which reduced the work considerably.

The expected number of sets of 16 entries that satisfy both (1) and (2) is

Choose(256, 16) · 0.000257 · sqrt(pmagic · fmod) ≈ 13.9 millions.

I used a collision method to search for about 67% of the possibilities. I found 9429609 sets, which is pretty close to what the random model predicted.

Next, for each combination of Latin squares that I tried, there were typically ~850 sets that fell on distinct rows, and an expected 0.08 sets that also fell on distinct columns. Each time there was a small probability of finding two compatible magic diagonals in the same square. Eventually I found an example.

64634874964
1704178542465427204
1944203742813431044
2324243643364437124
6794223142328432984
2146947241024
8124266842784439444
5954195542040428904
9284104441856442924
776487341552435894
680476541360431454
2442744841114
11904221042295426354
16244301643132435964
424784814934
13584252242619430074
736466744834464
800472545254504
3136428424205841964
3936435674258342464
4182486142829429524
3332468642254423524
7824161452945524
8504175457546004
4004200492542254
3684184485142074
1968498444551411074
156847844362648824
30384264641372425484
38134332141722431984
7754675435046504
7134621432245984
90348641376412474
1701416242592423494
1995419043040427554
2478423643776434224
2185422804323046654
989410324146243014
2714428324401248264
1863419444275445674
729429974648412964
1062443664944418884
38741591434446884
855435154760415204
30684165243658431864
24704133042945425654
21064113442511421874
1118460241333411614
13054144049049454
2117423364146415334
3074433924212422264
3277436164226423734
1752424824511416794
1080415304315410354
2712438424791425994
2544436044742424384
4181418084101749044
392241696495448484
270141168465745844
16654720440543604
28624328642756414844
30514350342938415824
1215413954117046304
19714226341898410224

By looking at the pattern of colors, can you guess what the compatibility condition is? This allowed me to permute the rows and columns to get a fully magic square.

329849284104442295463496422314679422104874429242635464232841856411904
1024776487343132417854272046942143016424654358943596417047241552416244
39444680476548142037431044266848124784281343145493419442784413604424
28904244274261942436437124195545954252243364411143007423242040448413584
1035439224169642938423364153341530410804350341464848415824211743154954430514
2352436841844172247254504686433324332145254207431984800422544851438134
5524196849844350428424196416147824675420584110746504313645294455147754
259942701411684117043392422264384242712413954212458446304307447914657412154
3014106244366429454162423494103249894133042592418884256541701414624944424704
567485543515413334236434224194441863460243776415204116142478427544760411184
600415684784432243567424641754850462142583488245984393645754362647134
243841665472041898436164237343604425444226342264360410224327747424405419714
826438741591425114190427554283242714411344304046884218741995440124344421064
2952440042004137246674464861441824264644834225425484736428294925430384
167944181418084275641440494542482417524328649049044148441305451141017428624
66547294299743658486412474228042185416524137641296431864903432304648430684

At the time of discovery, this 16×16 square was the smallest known magic square of 4th powers. The previous record was the 36×36 pentamagic square of Li Wen, found in 2008.

A 15×15 magic square of 4th powers

Starting from Christian Boyer's 15×15 semi-magic square of 4th powers, I applied the same method above to find magic diagonals.

Here's Christian's published semi-magic square, with the elements that will become diagonals highlighted:

24104244324584
10945454130841744431614
11145554133241776432194
763410904207142289428344
777411104210942331428864
144204384424524
888412214199842553427754
164224364464504
872411994196242507427254
1054252433646094214
4904117641568428424984
59541428419044345141194
98041862420584254846864
119042261424994309448334
21043994441454641474
130942142427374297549524
23143784483452541684
107841764422544245047844
3244432478342741354
1128415044272649444704
14524193643509412146054
17864197442444465849404
229942541431464847412104
51345674702418942704
217842783430254968413314
48646214675421642974
169242162423504752410344
5444986434417044084
1424425814894445410684
19684356741234615414764
18694231446234890416914
258343198486141230423374
71448844238434046464
282943075498441353422144
78248504272437446124
20474222547124979416024
17694614305473249764
191446643304792410564
36834127463541524420324
17164462466041254413864
330248894127042413426674
15864427461041159412814
3175410164139742286429214
15254488467141098414034
16504528472641188415184

Here's the square after the application of the Latin squares that I selected. This demontrates the importance of Latin squares when searching for diagonals.

24104244324584
10945454130841744431614
11145554133241776432194
763410904207142289428344
777411104210942331428864
144204384424524
888412214199842553427754
164224364464504
872411994196242507427254
1054252433646094214
4904117641568428424984
59541428419044345141194
98046864205842548418624
119048334249943094422614
21041474441454643994
130942737429754214249524
23144834525437841684
107842254424504176447844
7834432427432441354
2726415044944112844704
35094193641214145246054
254142299431464121048474
56745134702427041894
19744178642444494046584
169242350410344752421624
217843025413314968427834
48646754297421646214
5444344986417044084
1424489425814445410684
19684123435674615414764
16914231448904623418694
233743198412304861425834
64648844340423847144
307549844282941353422144
85042724782437446124
22254712420474979416024
73241769430546149764
792419144330466410564
15244368346354127420324
17164138644624125446604
330242667488942413412704
15864128144274115946104
2921422864101643175413974
14034109844884152546714
15184118845284165047264

Here's the fully magic square, after permuting the rows and columns:

142448942581437442724233743198485047824123046124106842583444548614
350941936412142164675419744178644864297424444621460546584145249404
10945454130844642247774111041643642109450431614288641744423314
544434498641353498441691423144307542829489042214440841869417046234
24104244255341221476341090488841998420714277545842834432422894
73241769430543175422864171641386429214101644624139749764660461412544
79241914433041525410984330242667414034488488946714105641270466424134
196841234356749794712464648844222542047434041602414764714461542384
490411764156843784483411904833423145254249941684984226142842430944
7834432427475242350425414229941692410344314642162413548474324412104
5954142841904417644225442104147410784245044414784411943994345145464
272641504494496843025456745134217841331470242783447041894112842704
105425243364214242737498046864130942975420584952421418624609425484
11145554133242507411994144204872419624384272543219452417764424
152443683463541650411884158641281415184528442747264203246104127411594

The magic sum is 794179 · 292965218 = 232666823866022. The largest entry is 294·1274 = 36834. This is the smallest possible magic sum and smallest possible largest entry using compatible taxicab(4,5,3) and taxicab(4,3,5). At the time of writing, this 15×15 square is the smallest known magic square of 4th powers. The previous record was my 16×16 square described earlier.

Other candidates for magic diagonals

Here are semi-magic squares that could lead to new records if their entries can be rearranged to form 2 magic diagonals:

Below is a calculation of the expected number of essentially distinct squares with magic diagonals according to the random model for these two candidates and for the three semi-magic squares I discussed earlier, sorted by power and by size.

powersemi-magic squaremagic sumcfmodpmagic · fmod · npermpmagic · fmod · nperm · nlatinmagic square
412×12 FL3.50×10131.8725.22.8×10-146.2×10-9none (FL)
15×15 CB2.33×10142.0017.72.6×10-1020found (FL)
16×16 FL6.28×10141.7814.16.1×10-1067found (FL)
525×25 FL4.75×10252.321.222.5×10-144.7×1017?
25×25 CB8.28×10252.653.021.5×10-142.9×1017?

The results show that a 25×25 magic square of 5th powers can almost certainly be obtained from the respective semi-magic squares, but to succeed we almost certainly have to use the flexibility afforded by the Latin squares.

Further generalization

In 2006, Christian Boyer found a method to construct an 8×8 semi-magic square of nth powers by using three taxicab(n,2,2) numbers. This hints at a general method to construct a (rst)×(rst) semi-magic square of nth powers by using a taxicab(n,r,s), a taxicab(n,s,t), and a taxicab(n,t,r). To push the idea further, maybe if we write k as the product of d factors, it is possible to create a semi-magic square of size k × k using d taxicabs? What about the Latin squares, how do they interact with this? Someone can try to figure it out!


page last updated: November 19, 2011