Counting digit

Paul Zimmermann Paul.Zimmermann at
Tue Mar 16 20:44:01 CET 2010


> 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.

Paul Zimmermann

More information about the gmp-discuss mailing list