Finding The Number of Digits

Some problems ask you to find the number of digits of an integer or variable. For example, the number 23 has two digits, and the number 1337 has four. But how many digits does an integer \(x\) have, if \(x^2\) has more than 2 but less than 4 digits?

There’s not yet enough information to determine exactly what \(x\) is, but a range is ascertainable. If all that’s known is that \(x\) is an integer and that \(x^2\) produces an integer that’s \(3\) digits long, then \(x\) is on the range \([10,31]\) as \(9^2=81, 10^2 = 100, 31^2=961,\) and \(32^2=1,024\). \(_\square\)

The wiki will discuss further and more rigorous ways to conduct this analysis.

Note that the numbers with precisely one digit are those integers in the range \( [1,9]\), the numbers with precisely two digits are those integers in the range \( [10, 99]\), and the numbers with precisely three digits are those integers in the range \( [100, 999]\), and so on. The two digit numbers are shown below:

However, determining the number of digits of an extremely large number can be somewhat tricky and is explored below.

#### Contents

## Large Numbers

A number will have precisely \(j\) digits if and only if it is in the range \(I_j = [10^{j-1}, 10^{j} – 1]\). For instance, the number \(5,000,000\) has \(7\) digits and is in the range \([10^{7-1},10^7-1] = [\text{1,000,000}, \text{ 9,999,999}].\)

Given an integer \(n\), one can determine \(j\), the number of digits in \(n\), by working with the inequality

\[ n\in I_j \implies 10^{j-1} \le n \le 10^j – 1.\]

Taking the base 10 logarithm gives

\[j-1 \le \log_{10} n < j,\]

so \(j-1 = \left\lfloor \log_{10} n \right\rfloor,\) where \(\lfloor x \rfloor\) is the floor function, denoting the greatest integer less than or equal to \(x\). Thus, \(j = \left\lfloor \log_{10} n \right\rfloor + 1\).

For any positive integer \(n\), the number of digits in \(n\) is \(\left\lfloor \log_{10} n \right\rfloor + 1\).

For example, take \(n = 5,000,000\), then \(\left\lfloor \log_{10} 5000000 \right\rfloor = 6,\) meaning that we expect this number to have \( \left\lfloor \log_{10} 5000000 \right\rfloor +1 = 7\) digits, which in fact it does.

## Examples

How many digits does \(100!\) have?

Using Stirling’s formula \(n! \approx \sqrt{2\pi n}\left(\frac ne \right)^n,\) we can get the number of digits of \(100!\) as follows:

\[\begin{align} N & = \lfloor \log_{10} {\color{blue}100!} \rfloor + 1 \\ & = \left \lfloor \log_{10} {\color{blue}\sqrt{2\pi \cdot 100}\left(\frac {100}e\right)^{100}} \right \rfloor + 1 \\ & = \left \lfloor \frac 12 \log_{10}2 + \frac 12 \log_{10} \pi + 1 + 100(2) – 100 \log_{10} e \right \rfloor + 1 \\ & = \lfloor 157.970 \rfloor + 1 \\ & = 158.\ _\square \end{align} \]