Let the number be n
then the number of digits in n
is given by:
math.floor(math.log10(n))+1
Note that this will give correct answers for +ve integers < 10e15. Beyond that the precision limits of the return type of math.log10
kicks in and the answer may be off by 1. I would simply use len(str(n))
beyond that; this requires O(log(n))
time which is same as iterating over powers of 10.
Thanks to @SetiVolkylany for bringing my attenstion to this limitation. Its amazing how seemingly correct solutions have caveats in implementation details.