I read recently that you can find the number of digits in a number through the formula $\lfloor \log_{10} n \rfloor +1$ What's the logic behind this rather what's the proof?
2 Answers
Suppose that $n$ has $d$ digits; then $10^{d-1}\le n<10^d$, because $10^d$ is the smallest integer with $d+1$ digits. Now take logs base $10$: $\log_{10}10^k=k$, so the inequality becomes $d-1\le\log_{10}n<d$. If you now take the floor (= integer part) of $\log_{10}n$, throwing away everything to the right of the decimal point, you get $\lfloor\log_{10}n\rfloor=d-1$. Thus, $d=\lfloor\log_{10}n\rfloor+1$. This isn’t quite what you posted, but it’s the integer part of it, and clearly the number of digits must be an integer.
- 67,568
- 43
- 308
- 617
- 631,399
-
So, given N=8 for example, how many digits would there be in log(8)? Thank you. – NoChance Nov 06 '12 at 23:35
-
2@Emmad: It’s not about the number of digits in $\log_{10}8$; the result is that $8$ has $\lfloor\log_{10}8\rfloor+1$ digits, which is true, since $\log_{10}8\approx0.90309$. – Brian M. Scott Nov 06 '12 at 23:38
-
I see now, thanks. – NoChance Nov 06 '12 at 23:42
-
@Emmad: You’re welcome. – Brian M. Scott Nov 06 '12 at 23:43
-
What are the purpose to take the floor of the log? – Hassam Abdelillah Apr 18 '19 at 14:22
-
1@HassamAbdelillah Because log returns a real number and digits is an integral value. – NetMage Sep 18 '19 at 21:26
-
2This only seems to work for non-zero positive integers. – NetMage Sep 18 '19 at 21:28
-
1@NetMage: That’s correct, but $\Bbb Z^+$ is generally the domain of interest, and $0$ is inherently a special case. If one really wants to include $0$, one can use $d=\lceil\log_{10}(n+1)\rceil$; the argument that justifies it for $n\in\Bbb Z^+$ doesn’t extend to $n=0$, but the formula works for $n=0$ anyway. – Brian M. Scott Mar 24 '22 at 04:41
-
@BrianM.Scott Why can one change the inequality to an equality when taking the floor of the logarithm? – jruota Aug 16 '23 at 16:27
-
2@jruota: By definition $\lfloor\log_{10}n\rfloor$ is the unique integer $k$ such that $k\le\log_{10}n<k+1$. We already know that $d-1\le\log_{10}n<d$; setting $k=d-1$, we have $k\le\log_{10}n<k+1$ and hence $\lfloor\log_{10}n\rfloor=k=d-1$. – Brian M. Scott Aug 22 '23 at 05:38
-
@BrianM.Scott Both your formulas do not work for n=0. The one with floor function returns minus infinity digits, the one with ceiling function (in comment) returns 0 digits. But the correct answer is 1 digit. This special case (n=0) should be handled separately. – 4LegsDrivenCat Jul 20 '24 at 17:56
-
@4LegsDrivenCat: As you’ve seen from my earlier comment, I don’t think that the case $n=0$ really needs to be handled at all, since $0$ is not in the natural domain of interest. The ceiling formula gives the number of digits that remain when all leading zeroes are removed, which is what I must have had in mind two years ago, but I agree that for $n=0$ this doesn’t agree with the minimum number of digits required to express the integer in writing. – Brian M. Scott Jul 20 '24 at 19:09
let's consider the binary base-2 system. Any decimal number (say 8) can be represented as $2^3$. So if you take $log_2(8)$ you get 3, which represents the number of bits to represent 8. Thus, any number between 8 and 16 can be represented with $\lceil log_2(N)\rceil$ bits and so on. You can easily extend this to digits.
- 980
- 7
- 14
-
6Your formula doesn’t work when $N$ is a power of $2$. It should be $\lfloor\log_2N\rfloor+1$. – Brian M. Scott Nov 06 '12 at 23:40