From New Scientist #1188, 3rd January 1980 [link] [link]
“You may remember,” said Mr Knull, “helping me recently to find the smallest possible set of three different integers P, Q and R such that P+Q, P+R, Q+R, P−Q, P−R and Q−R are all perfect squares. It was P=17, Q=8, R=−8.
“Here now is the harder version. Again you need to find P, Q and R satisfying the same conditions. But this time we don’t allow any of the three integers to be negative.”
Can you help me again, please, by finding the smallest set of three different integers which meet the conditions? By smallest I mean with P+Q+R as small as possible.
This puzzle refers back to Enigma 40.
A solution is given in New Scientist #1189, and in New Scientist #1190 it is noted that no correct solutions were recieved. However I think there is a solution where P+Q+R is smaller than that given as the solution in the magazine.
I sent New Scientist an email about this in December 2011, and in New Scientist #2853 an item was published in the Feedback section about my email, and I was awarded the prize for solving the puzzle 32 years late!
Note: This puzzle resurfaced as Project Euler Problem 142.
Note: This problem is known as Mengoli’s Six-Square Problem (1674). Euler found the smallest solution (1765), by hand.
[enigma45] [euler142]
It took me a couple of attempts before I found a satisfactory approach that would run in a sensible time. This Python program finds the published solution, and a “better” one in 1.6s (or 255ms using PyPy):
Solution: P=434657, Q=420968, R=150568, P+Q+R=1006193.
This satisfies the conditions of the puzzle and is smaller than the published solution of: P=733025, Q=488000, R=418304, P+Q+R=1639329. (Which you can find by running with a command-line argument greater than 1).
Here’s my algorithm coded up in BBC BASIC, running on emulated hardware from the 80’s. For the details see [ http://jimpulse.blogspot.com/2012/02/retrocomputing-enigma-45.html ].
This slight modification makes the program run in half the time (800ms to find 2 solutions, or 213ms to find just the first one), by caching the (a², b²) pairs where a²−b² is a perfect square, and then using those to find the (b², c²) pairs. It also includes code to make sure the solutions come out in strict P+Q+R order (which slows it down again).
I like programming and have just completed coding an algorithm of my own, to run in MATLAB. MATLAB is really far too powerful for the job! So how does an elapsed time of 13 minutes 38 seconds, for ‘a’ from 3 to 1000 sound?.
Will now look at the code above!
Tessa F
Here is an alternative Python implementation
Thanks for that. And for showing me the
[code]...[/code]
tags – I didn’t know those worked in WordPress. Although with a bit of digging I’ve found you can use[sourcecode language="python"]...[/sourcecode]
to get syntax highlighting too!An interesting find, Jim – can you edit the tags to add syntax highlighting to my post?
Yep. Done. I had a look to see if I could allow people to edit their own comments on wordpress.com, but I couldn’t find a setting for that.
To identify the actual squares in this enigma:
As P, Q, R = 434657 , 420968 , 150568 :
1) P + Q = 855625 (925 * 925)
2) P + R = 585225 (765 * 765)
3) Q + R = 571536 (756 * 756)
4) P – Q = 13689 (117 * 117)
5) P – R = 284089 (533 * 533)
6) Q – R = 270400 (520 * 520)
That’s interesting. I did write a MiniZinc model to try and solve this some time ago, but I didn’t put upper bounds on the integers, and also used constraints to check for squares, like this:
When I gave it to the [[
mzn-gecode
]] solver to chew on, it didn’t come back after 10 minutes, so I gave up on it.