I’ve got another interesing programming/mathematical problem.
For a given natural number q from interval [2; 10000] find the number n
which is equal to sum of q-th powers of its digits modulo 2^64.
for example: for q=3, n=153; for q=5, n=4150.
I wasn’t sure if this problem fits more to math.se or stackoverflow, but this was a programming task which my friend told me quite a long time ago. Now I remembered that and would like to know how such things can be done. How to approach this?
There are two key points,
Let us take a closer look at the case
q = 2. If ad-digit numbernis equal to the sum of the squares of its digits, thenand the condition
10^(d-1) <= d*81is easily translated tod <= 3orn < 1000. That’s not many numbers to check, a brute-force for those is fast. Forq = 3, the condition10^(d-1) <= d*729yieldsd <= 4, still not many numbers to check. We could find smaller bounds by analysing further, forq = 2, the sum of the squares of at most three digits is at most 243, so a solution must be less than 244. The maximal sum of squares of digits in that range is reached for 199: 1² + 9² + 9² = 163, continuing, one can easily find that a solution must be less than 100. (The only solution forq = 2is 1.) Forq = 3, the maximal sum of four cubes of digits is 4*729 = 2916, continuing, we can see that all solutions forq = 3are less than 1000. But that sort of improvement of the bound is only useful for small exponents due to the modulus requirement. When the sum of the powers of the digits can exceed the modulus, it breaks down. Therefore I stop at finding the maximal possible number of digits.Now, without the modulus, for the sum of the
q-th powers of the digits, the bound would be approximatelyso for larger
q, the range of possible solutions obtained from that is huge.But two points come to the rescue here, first the modulus, which limits the solution space to
2 <= n < 2^64, at most 20 digits, and second, the permutation-invariance of the (modular) digital power sum.The permutation invariance means that we only need to construct monotonous sequences of
ddigits, calculate the sum of theq-th powers and check whether the number thus obtained has the correct digits.Since the number of monotonous
d-digit sequences is comparably small, a brute-force using that becomes feasible. In particular if we ignore digits not contributing to the sum (0 for all exponents, 8 forq >= 22, also 4 forq >= 32, all even digits forq >= 64).The number of monotonous sequences of length
dusingssymbols issis for us at most 9,d <= 20, summing fromd = 1tod = 20, there are at most 10015004 sequences to consider for each exponent. That’s not too much.Still, doing that for all
qunder consideration amounts to a long time, but if we take into account that forq >= 64, for all even digitsx^q % 2^64 == 0, we need only consider sequences composed of odd digits, and the total number of monotonous sequences of length at most 20 using 5 symbols isbinom(20+5,20) - 1 = 53129. Now, that looks good.Summary
We consider a function
fmapping digits to natural numbers and are looking for solutions of the equationwhere
digitsmapsnto the list of its digits.From
f, we build a functionFfrom lists of digits to natural numbers,Then we are looking for fixed points of
G = F ∘ digits. Nownis a fixed point ofGif and only ifdigits(n)is a fixed point ofH = digits ∘ F. Hence we may equivalently look for fixed points ofH.But
Fis permutation-invariant, so we can restrict ourselves to sorted lists and considerK = sort ∘ digits ∘ F.Fixed points of
Hand ofKare in one-to-one correspondence. Iflistis a fixed point ofH, thensort(list)is a fixed point ofK, and ifsortedListis a fixed point ofK, thenH(sortedList)is a permutation ofsortedList, henceH(H(sortedList)) = H(sortedList), in other words,H(sortedList)is a fixed point ofK, andsortresp.Hare bijections between the set of fixed points ofHandK.A further improvement is possible if some
f(d)are 0 (modulo 264). Letcompressbe a function that removes digits withf(d)mod2^64 == 0from a list of digits and consider the functionL = compress ∘ K.Since
F ∘ compress = F, iflistis a fixed point ofK, thencompress(list)is a fixed point ofL. Conversely, ifclistis a fixed point ofL, thenK(clist)is a fixed point ofK, andcompressresp.Kare bijections between the sets of fixed points ofLresp.K. (AndH(clist)is a fixed point ofH, andcompress ∘ sortresp.Hare bijections between the sets of fixed points ofLresp.H.)The space of compressed sorted lists of at most
ddigits is small enough to brute-force for the functionsfunder consideration, namely power functions.So the strategy is:
dof digits to consider (bounded by 20 due to the modulus, smaller for smallq).ddigits.L, if it is,F(sequence)is a fixed point ofG, i.e. a solution of the problem.Code
Fortunately, you haven’t specified a language, so I went for the option of simplest code, i.e. Haskell:
It’s not optimised, but as is, it finds all solutions for
2 <= q <= 10000in a little below 50 minutes on my box, starting withAnd ending with
The exponents from about 10 to 63 take longest (individually, not cumulative), there’s a remarkable speedup from exponent 64 on due to the reduced search space.