I am trying to collect and then sum all the digits from a BigInt.
Example: sum all digits of 2^1000000
Intuitively, my first solution was using the
@time sum(digits(BigInt(2)^1000000)) 29.189925 seconds (3.15 M allocations: 50.253 GiB, 6.13% gc time) 1351546
However, I found this to be very inefficient. My second solution is much more efficient but less intuitive (converting a BigInt to a string and then parsing all chars):
@time sum(map(x -> parse(Int, x), collect(string(BigInt(2)^1000000)))) 0.062261 seconds (59.30 k allocations: 9.278 MiB, 8.69% gc time) 1351546
I guess that there should be a better way of doing this (e.g. accessing the internal representation of the BigInt)… or the
digits function is not optimized for BigInt.
Julia version 1.0.5 (2019-09-09)