Problem 80: Square root digital expansion

It is well known that if the square root of a natural number is not an integer, then it is irrational.
The decimal expansion of such square roots is infinite without any repeating pattern at all.

The square root of two is 1.41421356237309504880..., and the digital sum of the first one hundred decimal digits is 475.

For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.

My Algorithm

Dealing with floating-point numbers is hard, especially with the limited precision of C++.
Therefore my program performs all computations on integers only.
The main idea is: sqrt{2} = sqrt{2 * frac{1000000}{1000000}} = frac{sqrt{2 * 1000000}}{1000} = 1414
→ to find the first three fractional digits of sqrt{2} we need to "shift" 2 by 10^{2 * 3} = 10^6 = 1000000.

Computing the first 100 digits of sqrt{2} means that 2 must be converted to an integer with about 200 digits,
which is obviously too much for C++'s native data types.
It's showtime for my BigNum class - again ! About 2/3 of this solution is just BigNum code.

My first experiments were based on the Newton square root algorithm (see en.wikipedia.org/wiki/Newton's_method).
The code worked but was too slow (mainly because of several BigNum divisions).
Browsing through the internet people recommended a surprisingly simple and efficient digit-by-digit square root algorithm
nicely explained in a paper by Frazer Jarvis (see www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf).
That paper is quite accessible - read it if you know to know more about it.

My code can be found in the function jarvis. Its parameter precision is a huge number 10^{digits+extra}.
(due to the way this algorithm works, precision has 100 digits, not 2*100=200 digits).
I deliberately compute more digits than required because I use a trick for faster calculations:
only the square roots of prime numbers are actually computed.

For any composite number c = a * b where a and b are arbitrary factors of c, the square issqrt{c} = sqrt{a * b} = sqrt{a} * sqrt{b}
A single multiplication of two BigNums is much faster than a full square root computation.

I worked really hard to get 100% on this problem but I am stuck at 77.78%. Apparently only one person solved it in C++ so far.

My class BigNum isn't the fastest code. I used operator+= and operator*= instead of operator+ and operator* which helped a bit
but I still need to be about twice as fast for the two remaining test cases.

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 "2 100" | ./80

Output:

(please click 'Go !')

Note: the original problem's input 100 100cannot 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>

// store single digits with lowest digits first

// e.g. 1024 is stored as { 4,2,0,1 }

// only non-negative numbers supported

structBigNum : publicstd::vector<unsignedint>

{

// string conversion works only properly when MaxDigit is a power of 10

Benchmark

The correct solution to the original Project Euler problem was found in less than 0.01 seconds on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL)

Hackerrank

Difficulty

20%
Project Euler ranks this problem at 20% (out of 100%).

Hackerrank describes this problem as easy.

Note:Hackerrank has strict execution time limits (typically 2 seconds for C++ code) and often a much wider input range than the original problem.In my opinion, Hackerrank's modified problems are usually a lot harder to solve. As a rule thumb: brute-force is rarely an option.

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 !