# Getting all digits from a BigInt

**URL:** https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528
**Category:** Performance
**Created:** [March 4, 2020, 3:24pm UTC](https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528 "2020-03-04T15:24:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dmoura](https://avatars.discourse-cdn.com/v4/letter/d/f1d935/32.png) [@dmoura](https://discourse.julialang.org/u/dmoura)
#### Post date: [March 4, 2020, 3:24pm UTC](https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528/1 "2020-03-04T15:24:07Z")

</div>

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 `digits` function:

```julia
@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):

```julia
@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.

Any thoughts?  
Thanks!

Julia version 1.0.5 (2019-09-09)

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [March 4, 2020, 4:12pm UTC](https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528/2 "2020-03-04T16:12:24Z")

</div>

Indeed, the string solution is optimized for `BigInt`, calling out `mpz_get_str`, which is _the_ GMP API to get the digits. But it supports only limited bases.  
On the other hand, the Julia `digits` function is generic, but for such a huge bigint as in your example, uses a lot of intermediate bigints for the computation.

A solution could be to make `digits` use `mpz_get_str` when the base is supported (I think such a PR would be accepted).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 4, 2020, 4:55pm UTC](https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528/3 "2020-03-04T16:55:38Z")

</div>

You can save some memory, and a tiny bit of time and typing by doing

```julia
sum(parse(Int, c) for c in string(b)) 

```

instead of first allocating a string, then a character vector and then an integer array before summing.

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 4, 2020, 5:54pm UTC](https://discourse.julialang.org/t/getting-all-digits-from-a-bigint/35528/4 "2020-03-04T17:54:23Z")

</div>

If you’re going to do it repeatedly until you get a single digit, this is equivalent to reduction modulo 9.
