Searching for a Magic Square of Squares

I've wanted to try running software on the GPU for a couple years now, and had been considering ideas like drawing fractals or running neural networks. But after watching a recent Numberphile video on YouTube, I just had to see if I could apply the power to the GPU to searching for magic squares.

 

A "magic square" is a square arrangement of numbers where the rows, columns, and long diagonals all sum to the same value. Trivial examples include, of course, any square like:

555
555
555

where all the values are the same. Generally, we ignore any square containing any repeated digits.

As explained by Matt Parker in the Numberphile video, it is, counter intuitively, easier to find larger magic squares than smaller ones. This example 8×8 magic square he shows was first published in 1890!

56348571847931
332054487295910
264313236438449
195353053124660
152563241245040
655171136583245
611642522713922
446228371451213

Pfeffermann, 1890. All rows, columns, and diagonals sum to 260.

This square also has another property that makes it even more special: it remains a magic square when all the values are squared! All rows, columns, and diagonals sum to 11,180 when the values are squared.

The first known 4×4 magic square of squares was discovered by Euler in 1770:

68²29²41²37²
17²31²79²32²
59²28²23²61²
11²77²49²

Euler, 1770. All rows, columns, and diagonals sum to 8,515.

The Challenge

It's apparently so much harder to find a 3×3 magic square of squares than a 4×4 that nobody has ever done it, and it's possible that none exist. This page has a detailed breakdown of the search.

Toward the end of the video a $10,000 prize was announced for anyone who finds a 3×3 square of squares, but for me it was fun just to see whether a GPU-based program would be faster than a CPU-based one.

After some experimentation with rust and the metal crate, I've got both CPU and GPU solutions working on my Apple Silicon Mac. My algorithm is a simple exhaustive search with "checkpointing" so I can halt and resume the search as needed. The GPU one seems to run about 2.4x faster than the CPU one.

My plan is to run a search with a relatively small maximum root, and then, depending on how long that takes, run another search with a larger maximum root (without repeating any previously examined squares). I've allowed the search to return any "near miss" squares found, where one row or column or diagonal doesn't have the same sum as the rest. I'll list my program's best found squares below.

3×3 Status and "Near Miss" Squares

While searching for magic square solutions, we'll come across many "near miss" squares with one row, column, or diagonal sum that doesn't match the others. I'll list the ones my program finds below. Many of them of course have been previously found, but I'd still like to list them here as my program comes across them. I'll try to omit any squares that mirror or rotate an already listed one.

March 31, 2025:

94²113²
127²58²46²
74²97² 82²

Sums of 21,609 with one diagonal sum of 10,092.

April 1, 2025:

188²226²
254²116²92²
148²194²164²

Sums of 86,436 with one diagonal sum of 40,368.

April 2, 2025:

46²82²113²
127²74²
58²97²94²

Sums of 21,609 with one diagonal sum of 16,428.

April 2, 2025:

58²46²127²
94²113²
97²82²74²

Sums of 21,609 with one diagonal sum of 38,307.

April 3, 2025:

92²164²226²
254²148²
116²194²188²

Sums of 86,436 with one diagonal sum of 65,712.

April 3, 2025:

116²92²254²
188²226²
194²164²148²

Sums of 86,436 with one diagonal sum of 153,228.

April 3, 2025:

My exhaustive search with a maximum root of 256 has completed. I am pretty confident there are no magic squares of squares where all the values (roots) are less than or equal to 256.

April 5, 2025:

282²339²
381²174²138²
222²291²246²

Sums of 194,481 with one diagonal sum of 90,828.

April 6, 2025:

376²452²
508²232²184²
296²388²328²

Sums of 345,744 with one diagonal sum of 161,472.