No one has discussed the version of BigInteger. To do this, I would look at 10 1 10 2 10 4 10 8 and so on until you find the last 10 2 n which is less than your value. Take the number div and mod 10 2 n to get 2 smaller values. Rinse, rinse and repeat recursively. (You must save your iterated squares of 10 in an array, and in the recursive part pass information about the next power for use.)
With BigInteger with numbers k, dividing by 10 is O (k). Therefore, to find the sum of the digits with a naive algorithm is O (k 2 ).
I donβt know what C # uses internally, but non-naive algorithms there for multiplying or dividing k-bits by a k-bit integer all work in O (k 1.6 ) time or better (most of them are much better, but have overhead, which makes them worse for "small large integers"). In this case, preparing the initial list of degrees and splitting times takes O (k 1.6 ) time. This gives you 2 problems of size O ((k / 2) 1.6 ) = 2 -0.6 O (k 1.6 ). At the next level, you have 4 problems of size O ((k / 4) 1.6 ) for another 2 -1.2 O (k 1.6 ) Work. Add all terms and powers of 2 to a geometric series converging to a constant, so the total work is O (k 1.6 ).
This is a definite victory, and the victory will be very, very obvious if you work with numbers on thousands of numbers.
source share