Problems & Puzzles: Puzzles

Problem 93. A couple of SG 3x3 magic squares composed only by distinct palprimes

Around January of the year 2001, my friend Jaime Ayala constructed the following pair of 3x3 magic squares, composed by 18 distinct integes palindromic & Sophie Germain-alike from one square to the other, but only a few (4) of them being primes. This was the starting point of my puzzle 124.

Here is the pair of SG alike 3x3 magic squares, gotten by Jaime Ayala

Palindromic but not all primes in a Sophie-Germain-alike pair of Magic Squares, by Jaime Ayala, January 2001
252 171 363 --> 505 343 727*
373* 262 151* 2.n+1 747 525 303
161 353* 272 --> 323 707 545

The 18 integers are palindromes but only 4 of these integers are primes*.

Very optimistic we posed the following only & apparently simple question:

Can you produce one of this type of pairs of Magic squares (SG alike) , but using exclusively (distinct) palindromic prime numbers?

...

The response of our readers was a noisy silence... Until just 25 years later, exactly the past May 15, 2026, Arina Bator one young Polish student of the Jagiellonian University in Cracovia, sent an almost solution to this old puzzle:

Almost Palprime &  SG Magic Squares by Arina Bator, May 2026
1925356557556535291 1529480708070849251 1727253646463527271
1529260726270629251    1727363637363637271 1925466548456645291
1727473628263747271 1925246566656425291 1529370717170739251
     
3850713115113070583 3058961416141698503 3454507292927054543
3058521452541258503 3454727274727274543 3850933096913290583
3454947256527494543 3850493133312850583 3058741434341478503
Qualification =13/18 72.22%
Fails = 5 integers
Integers that are Palindromes but not Primes
Integers are neither Primes nor Palindromes

We have learned that each puzzle finds out finally his appropriate puzzler. Arina Bator will be known as the first producer of a good near-miss solution of this puzzle.

Q. Can you produce a complete solution to this puzzle or at least a solution with greater Qualification than 72.22%?

 


From May 23 to May 29, 2026, contriburions came from Michael Branicky, Paul Cleary, Oscar Volpatti

***

Michael wrote:

I found a numbers with the same qualification: 13/18 (Qualification 72.22%)

Here are two of them.

1526281538351826251 1608474946494748061 1447691427241967441
1448892526252988441 1527482637362847251 1606072748472706061
1607273847483727061 1446490328230946441 1528683736373868251*

3052563076703652503 3216949892989496123 2895382854483934883**
2897785052505976883** 3054965274725694503 3212145496945412123
3214547694967454123 2892980656461892883** 3057367472747736503**

==
15150545392929354505151 15180649091719094608151 15150447393839374405151* 15160449293739294406151* 15160547292829274506151 15160645291919254606151
15170647191819174607151 15140445493939454404151 15170549192729194507151

30301090785858709010303 30361298183438189216303 30300894787678748810303** 30320898587478588812303** 30321094585658549012303 30321290583838509212303 30341294383638349214303 30280890987878908808303** 30341098385458389014303

* Integers that are Palindromes but not Primes
** Integers are neither Primes nor Palindromes

***

Paul wrote:

During the week I found a few 13 out of 18 pp's and sometimes 1 and 2 others that were just palindromes, the number of digits ranged from 19 to 21.  My last attempt with digit length 23 produced a 14 out of 18 solution (Qualification 77.77%), my program has another 24 hours or so to run so I doubt I will meet your deadline if any better solutions appear.  Here is my 14 solution.
Magic sum = 111818481812121818481819

37789722330808322248778 38160917070507071906183 35867842410806424326858
35350947350705374905353 37272827270707272827273 39194707190709170749193
38677812130608121327688 36384737470907473748363 36755932210606223405768

and 

Magic sum = 223636963624243636963641

75579444661616644497557 76321834141014143812367 71735684821612848653717
70701894701410749810707 74545654541414545654547 78389414381418341498387
77355624261216242655377 72769474941814947496727 73511864421212446811537


as a picture with colour.

(I responded to Paul that whenever his code finish I will add his last results.)

***

Oscar wrote:

Problem 93 was very challenging!
I wasn't able to find a complete solution. My best solutions so far have qualification 15/18 = 83.33%.
The smallest such solution was somehow unexpected: I assumed that my search algorithm would generate candidate squares with odd entries only, but I didn't explicitly forbid even entries.
So, "base" magic square of this solution happens to contain three even non-palindrome composites, placed on main diagonal; the remaining 15 entries are palprimes.
 
36656777836768827765668  39354717172927171745393  37261838473637483816273
38362838464646483826383  37757777827777827775778  37152717190909171725173
38253717181918171735283  36160838482628483806163  38858777818786827785888
 
73313555673537655531337  78709434345854343490787  74523676947274967632547
76725676929292967652767  75515555655555655551557  74305434381818343450347
76507434363836343470567  72321676965256967612327  77717555637573655571777
 
I found 154 more solutions with qualification 15/18, involving numbers with up to 35 digits.
Two of them are remarkable because they are the most similar to Jaime Ayala's construction: in both cases, the 18 integers are odd palindromes, and 15 of them are also prime.
Solution involving six Sophie Germain palprime pairs:

15163525171839293817152536151  17192545172705450727154529171  16164535170937073907153546161
17174545170925052907154547171  16173535171827272817153537161  15172525172729492727152527151
16182535172717471727153528161  15154525170949094907152545151  17183545171815251817154538171

30327050343678587634305072303  34385090345410901454309058343  32329070341874147814307092323
34349090341850105814309094343  32347070343654545634307074323  30345050345458985454305054303
32365070345434943454307056323  30309050341898189814305090303  34367090343630503634309076343

Solution involving seven Sophie Germain palprime pairs:

350529381637174636471736183925053  350737393547350525053745393737053  350618372727292747292727273816053
350717373727390747093727373717053  350628382637272636272736283826053  350539391547154525451745193935053
350638392547252525252745293836053  350519371727194747491727173915053  350727383637370636073736383727053

701058763274349272943472367850107  701474787094701050107490787474107  701236745454585494585454547632107
701434747454781494187454747434107  701256765274545272545472567652107  701078783094309050903490387870107
701276785094505050505490587672107  701038743454389494983454347830107  
701454767274741272147472767454107
 
I implemented the clever search strategy described by Arina Bator.
The entries of every magic square can be arranged into three arithmetic progressions with common difference r.
Corresponding elements from each sequence also form three more arithmetic progressions with common difference d. 
A "greedy" strategy allows to generate candidate solutions with qualification at least 12/18.
Detect six Sophie Germain palprime pairs (x1a,y1a), (x2a,y2a), (x3a,y3a), (x1b,y1b), (x2b,y2b), (x3b,y3b)  
such that triplets (x1a,x2a,x3a) and (x1b,x2b,x3b) are both arithmetic progressions with common difference r.
Choose integer x1c in order to form a three-term arithmetic progression with x1a and x1b.
Generate arithmetic progression (x1c,x2c,x3c) with common difference r.
Fill "base" magic square Mx, generate "transformed" magic square My, compute qualification. 

But I added an explicit constraint: the 18 integers of a candidate solution must have the same (odd) number of digits.
I'm not sure if such constraint is necessary or not, but it produces nicer solutions.
Implementation details.
Step 0: choose the desired number of digits 2*k+1 (odd, otherwise all palindromes are divisible by 11).
Step 1: search for all Sophie Germain palprime pairs (x,y).
There are 4*10^k palindromes x ending with digit 1,3,7, or 9; but if x starts with 7 or 9, then number y = 2*x+1 has one more digit than x, so discard them.
In general, if y is also a palindrome with 2*k+1 digits, then the digits of x and y must follow specific patterns:
digits of x: low, high, low, ... , high, low;
digits of y: odd, even, odd, ... , even, odd;
where "low" means "from 0 to 4" and "high" means "from 5 to 9".
So there are only 2*5^k candidate pairs of palindromes (x,y); check them all and save the pairs whose members are both prime.
This is the most time-consuming step.
Step 2: among saved palprimes x, search for all triplets (x1,x2,x3) forming an arithmetic progression.
The idea is to consider every possible pair (x1,x3) and check if the mean value x2=(x1+x3)/2 is also a saved palprime.
But x2 will be a palindrome with the correct digit pattern only if every digit in x3 has the same parity of the corresponding digit in x1.
Divide saved palprimes x in 2^k distinct buckets according to digit parity pattern, then consider only pairs (x1,x3) of palprimes from the same bucket.
For every arithmetic progression (x1,x2,x3) found, save the common difference r=x2-x1=x3-x2, along with the starting term x1 of the progression.
Step 3: compare saved pairs (r,x1) and search for repeated values of r.
It suffices to sort saved pairs by increasing r and then by increasing x1. For each saved difference r, group together all corresponding starting terms x1 saved.
If resulting group has only one element, discard r. Otherwise, consider every possible pair (x1a, x1b) of elements from the group.
Term x1c can be chosen in three ways, depending on which of x1a, x1b, x1c will be the middle term of the resulting arithmetic progression. 
x1c = (x1a+x1b)/2;
x1c = 2*x1a-x1b;
x1c = 2*x1b-x1a.
My laptop computed all Sophie Germain palprime pairs (x,y) with at most 39 digits in less than one day.
However, starting form 37 digits, it seems that available memory (2GB) was not enough to store all necessary data for detecting arithmetic progressions.

***

On June 2, 2026, Oscar wrote again.

About puzzle 93, I detected a little, malicious bug within my search program.
Step 1 always considered at most 2^32 palindromes to test for primality, due to an index variable incorrectly declared as a 32-bit integer.
Following computations seem to be correct.
But 2*5^13 < 2^32 < 2*5^14, so the search was exaustive only up to 27 digits.
From 29 digits to 39 digits, a smaller and smaller fraction of candidates was collected, with a running time never exceeding two or three hours.
After fixing the bug and running some simulations, I estimated the following execution times on my laptop:
29 digits -> 12 hours;
31 digits -> three days;
33 digits -> two weeks;
35 digits -> two months;
37 digits -> one year;
39 digits -> five years.
So I would have never been able to compute "all Sophie Germain palprime pairs (x,y) with at most 39 digits in less than one day".
I apologize for such naive, bold claim!

I slightly modified my program, then I started a new search up to 29 digits, which took 15 hours to complete.
Two magic squares with qualification 16/18 = 88.89% were detected.  
In both cases, all 18 entries are 29-digit odd palindromes and 16 of them are also prime.
Solution 1:
16263929290826462809292936261  18351527090916261909072515381  17172728190736363709182727171
18171527090736263709072517181  17262728190826362809182726271  16353929290916461909292935361
17352728190916361909182725371  16173929290736463709292937161  18261527090826262809072516281

32527858581652925618585872523  36703054181832523818145030763  34345456381472727418365454343
36343054181472527418145034363  34525456381652725618365452543  32707858581832923818585870723
34705456381832723818365450743  32347858581472927418585874323  36523054181652525618145032563

Solution 2:
15270936074727272747063907251  19494748274837173847284749491  17082827174617371647172828071
19094728274617371647282749091  17282837174727272747173828271  15470946074837173847064907451
17482847174837173847174828471  15070926074617371647062907051  19294738274727272747283749291

30541872149454545494127814503  38989496549674347694569498983  34165654349234743294345656143
38189456549234743294565498183  34565674349454545494347656543  30941892149674347694129814903
34965694349674347694349656943  30141852149234743294125814103  38589476549454545494567498583

Finally, I found a solution with all palindromes and qualification 15/18, much smaller than my previous submissions:
1527162628382838262617251  1748493706363636073948471  1606161517371737151616061
1706271506361636051726071  1627272617372737162727261  1548273728383838273728451
1648383717373737173838461  1506051528381838251506051  1727382606362636062837271

3054325256765676525234503  3496987412727272147896943  3212323034743474303232123
3412543012723272103452143  3254545234745474325454523  3096547456767676547456903
3296767434747474347676923  3012103056763676503012103  3454765212725272125674543

***

Records   |  Conjectures  |  Problems  |  Puzzles