Math Fundamentals18 sections · 814 units
Open in Course

Logarithms and Number of Digits (Base 10)

Counting digits

The number of digits in nn (base 1010) is ⌊log⁡10(n)⌋+1\lfloor \log_{10}(n) \rfloor + 1. For example, n=1234n = 1234 has 44 digits, and log⁡10(1234)≈3.09\log_{10}(1234) \approx 3.09.

This works because 10k−1≤n<10k10^{k-1} \leq n < 10^k, so k=⌊log⁡10(n)⌋+1k = \lfloor \log_{10}(n) \rfloor + 1. You can compute the number of digits without converting to a string.

Algorithms that process digits (like summing digits or reversing a number) run in O(log⁡10n)O(\log_{10} n) time, which is the same as O(log⁡n)O(\log n) in Big-O notation.