Problem 284: Steady Squares

The 3-digit number 376 in the decimal numbering system is an example of numbers with the special property that its square ends with the same digits: 376^2 = 141376.
Let's call a number with this property a steady square.

Steady squares can also be observed in other numbering systems. In the base 14 numbering system, the 3-digit number c37 is also a steady square: c37^2 = aa0c37,
and the sum of its digits is c+3+7=18 in the same numbering system.
The letters a, b, c and d are used for the 10, 11, 12 and 13 digits respectively, in a manner similar to the hexadecimal numbering system.

For 1 <= n <= 9, the sum of the digits of all the n-digit steady squares in the base 14 numbering system is 2d8 (582 decimal).
Steady squares with leading 0's are not allowed.

Find the sum of the digits of all the n-digit steady squares in the base 14 numbering system for1 <= n <= 10000 (decimal) and give your answer in the base 14 system using lower case letters where necessary.

My Algorithm

This problem bugged me for a while ... the amount of code written for my solution was astonishing (and only a fraction of it survived ...).

Continuing with two digits caused a problem:
I couldn't find a digit that can be prepended to 1 - but there are such digits for 7 and 8.
As it turns out, I can "extend" 7 and 8 basically to infinitely long steady squares.
And there is always exactly one digit I can prepend to a steady square such that it becomes a larger steady square.
The BigNum class from my toolbox already supported different bases.
Adding a toString() method for base 14 was straightforward.
Then I wrote isSteady(): it multiplies the number with itself and in each step checks whether the last digits remain unchanged.

if the last m+1 digits of t are identical to s' then I found the correct digit

It works pretty well for up to 100 digits.

The Wikipedia page en.wikipedia.org/wiki/Automorphic_number focusses on an algorithm based on the Chinese Remainder Theorem.
And I don't really like it ... so it looked at the output and saw that there is a certain relationship between the first steady squares of 7:s_1 = 7_14 = 7_10s_2 = 37_14 = 49_10 = 7^2s_3 = c37_14 = 2401_10 = 49^2 = 7^4s_4 = 0c37_14 = 2401_10 = 49^2 = 7^4s_5 = a0c37_14 = 386561_10 = 7^5 * 23
Unfortunately it doesn't work for s_4 and s_5 anymore, so I stopped working and solved other Project Euler problems.Problem 455 isn't related to the current problem but it reminded my that modulo often works in strange ways.
And indeed:s_4 = s^2_3 mod 14^4 = 2401^2 mod 14^4 = 5764801 mod 38416 == 2401s_5 = s^2_4 mod 14^5 = 2401^2 mod 14^5 = 2401 mod 537824 == 386561
So I have a simple way to generate all digits step-by-step.

My multiplication code was extremely slow: the square of a number with m digits has 2m digits.
But I only need m + 1 of those - so I wrote multiplyLow() which stops after enough digits were computed and discards the rest.

The Wikipedia mentions a smart way to generate the second automorphic number: "the sum of two automorphic numbers is 10^k + 1".
If I have one automorphic number s then the other one is S = 10^k + 1 - s. My findOther() function does exactly that.
The solution for 1000 digits takes half a second and the solution for 10000 digits is found after about 8 minutes.

But another formula on the Wikipedia page caught my attention (I didn't realize its importance when I read it the first time):n' = (3n^2 - 2n^3) mod 10^{2k}
Starting with an automorphic number n I get a new automorphic number n' that has twice as many digits.
Even though this formula is intended for base 10 numbers it still works for base 14 (I have no idea about other bases).

the last digit is part of all steady squares, that means it appears 10000 times

the second-to-last digit is part of all steady squares, except the steady square with only one digit, that means it appears 9999 times

... and so on ...

but any steady square with a leading zero must be discarded → I subtract all digits in a simple loop (looks slow, but it surprisingly fast)

Alternative Approaches

Dedicated large integer library such as GMP are much faster than my simple BigNum class.
With a clever use of residues you can get away with simple integer arithmetic as well and solve the problem in less than 0.1 seconds.

Note

This solution currently shares the "first place" of most code I needed to solve a problem, see my ranking.
Despite all the code (and time) needed to crack the problem, I felt that this was a really nice one.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):

This is equivalent toecho 9 | ./284

Output:

(please click 'Go !')

Note: the original problem's input 10000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

#include<iostream>

#include<vector>

#include<string>

// ---------- code taken from my toolbox, with a few modifications / extensions, see explanations above ----------

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !