From New Scientist #1192, 31st January 1980 [link] [link]
Young James was playing with his new calculator when a fault in the circuitry caused only the last four digits of the display to light up. Nothing else appeared to be wrong until he fed in a certain four-figure number. When he pressed the “square” button the number was unchanged, and on pressing the “square root” button the number again failed to change.
He showed this curious result to his elder sister, Camilla, who, after a calculation, was able to tell him that he happened to have picked the only four-figure number which when squared ends in the same four digits in the same order.
What was the number?
[enigma49]
It’s very straightforward to write a program that checks all 4-digit numbers, but here’s a program that computes increasing n-digit numbers, so you can use it to find much larger numbers if you like. It takes 35ms to find the 4-digit solution.
Solution: The number was 9376.
More details on Automorphic Numbers can be found at Wikipedia and Wolfram Alpha.
Interestingly the number 9376 corresponds to ZERO on a phone keypad.
A generalized version for different powers and length
z2 = z mod 10N → z(z − 1)= 0 mod 10N. One factor of z(z − 1) is odd, the other even. The even factor is a multiple of 2N.
For a solution other than 0 or 1, the odd factor of z(z − 1) must be a multiple of 5N (if the even factor is a multiple of 5 it is a multiple of 10, so the odd factor is congruent to 1 or 9 mod 10 and therefore not a multiple of 5, so the even factor is a multiple of 10N).
So the solution is one of
a) z = 2Nx, z − 1 = 5Ny
b) z − 1 = 2Nx, z = 5Ny
Solutions to the Diophantine equation 2Nx + 5Ny = 1 produce the solutions
a) z = 2N(x mod 5N)
b) z = 5N(y mod 2N)
So, some code. Function egcd is a standard recursive implementation of the Extended Euclidean Algorithm to find solutions to a linear Diophantine equation (and the gcd as a by-product).
The formatting didn’t come out as I had hoped. There should be some superscripts in the text. I have posted a properly formatted version on my blog
That’s a neat bit of analysis, and an efficient solution.
(I think I’ve fixed up the superscripts for you).
Thanks for fixing the superscripts, Jim.
As it turns out, a recent Project Euler problem, number 407 has some similarities to Enigma 49 (but is more generalised).
Should the Diophantine equation read something like x2^n – y5^n = ±1
(expressing the difference between z and z – 1),
or is there a mental leap that was too great for me?
As you see, I’ve avoided those troublesome superscripts!
We can use the general Alphametic expression solver [[
SubstitutedExpression()
]] from the enigma.py library to solve this puzzle without needing to write a program.Here’s the command and its output:
The command runs in 80ms.
We use the [[
--distinct=""
]] parameter to allow different letters to stand for the same digit.