|
| 1 | +# Finding the number of digits in a number |
| 2 | + |
| 3 | +Let's say `N = 2019`. The number of digits in N here is 4 and the digits are: `2`, `0`, `1`, and `9`. |
| 4 | + |
| 5 | +Some more Examples: |
| 6 | +``` |
| 7 | +N = 00 [zero] |
| 8 | +Number of digits = 0 |
| 9 | +
|
| 10 | +N = -123 [negative] |
| 11 | +Number of digits = 3 |
| 12 | +
|
| 13 | +N = 10000 [positive] |
| 14 | +Number of digits = 5 |
| 15 | +``` |
| 16 | + |
| 17 | +<div align="center"> |
| 18 | + <h2 align="center"> 1st Solution </h2> |
| 19 | +</div> |
| 20 | + |
| 21 | +### Simple Solution |
| 22 | +The first solution that comes to mind is quite simple: |
| 23 | + |
| 24 | + 1. Check whether the number N is equal to zero. |
| 25 | + 2. Increase the count of digits by 1 if N is not zero. |
| 26 | + 3. Reduce the number by dividing it by 10. |
| 27 | + 4. Repeat the above steps until the number is reduced to zero. |
| 28 | + |
| 29 | +**Analysis of the above algorithm:** You can clearly see that the number of operations performed in the above solution is equal to the count of digits present in the number. So, the time complexity of the solution is O(digitsCount). |
| 30 | + |
| 31 | +**Dry-run of the above algorithm:** |
| 32 | +Consider an example, N = 58964. Initialize a variable digitsCount to zero which will store the count of digits. Keep incrementing digitsCount until N is not zero, and reduce it by dividing by 10 at each step. |
| 33 | +``` |
| 34 | +Iteration 1: N not equals to 0 |
| 35 | +Increment digitsCount, digitsCount = digitsCount + 1. |
| 36 | +digitsCount = 0 + 1 = 1. |
| 37 | +N = N/10 = 58964/10 = 5896. |
| 38 | +
|
| 39 | +Iteration 2: N not equals to 0 |
| 40 | +Increment digitsCount, digitsCount = digitsCount + 1. |
| 41 | +digitsCount = 1 + 1 = 2. |
| 42 | +N = N/10 = 5896/10 = 589. |
| 43 | +
|
| 44 | +Iteration 3: N not equals to 0 |
| 45 | +Increment digitsCount, digitsCount = digitsCount + 1. |
| 46 | +digitsCount = 2 + 1 = 3. |
| 47 | +N = N/10 = 589/10 = 58. |
| 48 | +
|
| 49 | +Iteration 4: N not equals to 0 |
| 50 | +Increment digitsCount, digitsCount = digitsCount + 1. |
| 51 | +digitsCount = 3 + 1 = 4. |
| 52 | +N = N/10 = 58/10 = 5. |
| 53 | +
|
| 54 | +Iteration 5: N not equals to 0 |
| 55 | +Increment digitsCount, digitsCount = digitsCount + 1. |
| 56 | +digitsCount = 4 + 1 = 5. |
| 57 | +N = N/10 = 5/10 = 0. |
| 58 | +
|
| 59 | +Iteration 6: N becomes equal to 0. |
| 60 | +Terminate any further operation. |
| 61 | +Return value of digitsCount. |
| 62 | +
|
| 63 | +Therefore, number of digits = 5. |
| 64 | +``` |
| 65 | + |
| 66 | +<div align="center"> |
| 67 | + <h2 align="center"> 2nd Solution </h2> |
| 68 | +</div> |
| 69 | + |
| 70 | +### Better Solution |
| 71 | +A better solution is to use mathematics to solve this problem. The number of digits in a number say N can be easily obtained by using the following formula: |
| 72 | +``` |
| 73 | +number of digits in N = log10(N) + 1. |
| 74 | +``` |
| 75 | + |
| 76 | +**Derivation:** Suppose the number of digits in the number **N** is **K**. |
| 77 | + |
| 78 | +Therefore, we can say that: |
| 79 | +``` |
| 80 | +10^(K-1) <= N < 10K |
| 81 | +``` |
| 82 | +Applying base-10 logarithm to both sides in the above equation, we get: |
| 83 | +``` |
| 84 | +K-1 <= log10(N) < K. |
| 85 | +
|
| 86 | +or, K - 1 + 1 <= log10(N) + 1 < K + 1 |
| 87 | +or, K <= log10(N) + 1 < K + 1 |
| 88 | +``` |
| 89 | +Therefore, |
| 90 | +``` |
| 91 | +K = floor(log10(N) + 1) |
| 92 | +``` |
| 93 | + |
| 94 | +**Analysis of the above algorithm:** The above algorithm uses two mathematical functions: the `logarithm of a number` and the `floor function`. Therefore, its time complexity depends on the complexity of those two functions. Practically, the `floor function` is always, or can at least easily be made, constant time - all it has to do is: drop the digits behind the decimal point. For practical purposes, one can `assume that the logarithm is constant time` as well, as one will usually be working with fixed-width floating-point values. If one uses `arbitrary-precision "big number" libraries` however, logarithm will not be constant anymore: performance will depend on the logarithm algorithms used. |
| 95 | + |
| 96 | + |
| 97 | +# Source |
| 98 | + |
| 99 | +- [Proof in GeeksForGeeks](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods/) |
| 100 | + |
| 101 | +# YouTube |
| 102 | + |
| 103 | +- [Video URL](https://www.youtube.com/watch?v=ngWnvWR8NkE) |
0 commit comments