# Counting digit

Décio Luiz Gazzoni Filho decio at decpp.net
Tue Mar 16 22:42:53 CET 2010

```On Mar 16, 2010, at 4:44 PM, Paul Zimmermann wrote:

>
>       Giovanni,
>
>> I must count digits "1" in a base 18 number.
>> Does'it exist a function in Gmp, like DigitCount function in Mathematica?
>
> there is mpz_sizeinbase, but it might be wrong by 1 for base 18:
>
> -- Function: size_t mpz_sizeinbase (mpz_t OP, int BASE)
>     Return the size of OP measured in number of digits in the given
>     BASE.  BASE can vary from 2 to 62.  The sign of OP is ignored,
>     just the absolute value is used.  The result will be either exact
>     or 1 too big.  If BASE is a power of 2, the result is always
>     exact.  If OP is zero the return value is always 1.
>
> You might also use mpz_get_str and strlen for an exact result.

I think he means counting occurrences of the digit 1 in the base-18 expansion of the number (that's what his explanation sounds like, no idea what DigitCount in Mathematica does). Short of doing a base conversion (via mpz_get_str) and a while loop which counts 1s, I don't see a way to do it.

Décio
```