Pages

Monday, May 23, 2011

Counting the Digits in a Sequential Integer Series

Last year, I developed a way to count the number of digits in a sequential integer series without having to count the digits in each number. I am still not entirely sure why I pursued this, I think I was working on some programming project that got me thinking about how long a file would be if I wrote sequential numbers to it. Anyway, it was an interesting project; I learned a lot about the summation operation, among other things. Less wonderful was letting this project lay fallow: writing how this equation works has been challenging, to say the least. But, you are probably not really interested in the how, but more of the what I am talking about.

To be clear, $\lfloor x \rfloor = \text{floor}(x)$. Also, this is nothing to do with the digit sum.

Before the equations, here is what I am trying to accomplish on a higher level. This equation counts the amount of digits in a number series. It does not add the numbers together, just the digits representing the numbers. Of course, this assumes you are operating in base 10, but could, no doubt, be extended to all bases.


If you have an array containing all the numbers from 1 to 20, inclusive, and count all the digits of those numbers, you accomplish exactly what this function does.

c = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
f(x) = The sum of digits in the series.
f(c) = 31

Scaling down the amount of numbers, but increasing the digits in each number, here is another example.

d = {1430,1431,1432}
f(d) = 12

Conceptually, this is fairly easy to grasp, but the math is both elegant and convoluted, depending on how fast you need to calculate the result. It was not hard to find that
$$(1)  f(x) = \sum_{i=1}^{x}\left(\lfloor\log i\rfloor + 1\right)$$
produces the answer we are looking for. However, what if you want to calculate large x values? $f(240,000)$ takes a long time to come up with the answer, even for my powerful desktop. The amount of digits between 1 and 240,000 equals 1,328,895, by the way.

Thankfully, there are patterns we can use to help cut down on the computational expense. For instance, the number of digits on the interval $10^n < x < 10^{n+1}-1$, has...well, it is easier to explain with a table.

Table 1
$n=\lfloor\log x\rfloor$
$10^n$
$10^{n+1}-1$
Numbers
Digits
0
1
9
9
9
1
10
99
90
180
2
100
999
900
2700
3
1000
9999
9000
36000

As you can see, the digits increase at a rate of $9n(10^{n-1})$. Calculating the digits from 1 to 9999 is a simple summation.
$$(3)  T(x)=\sum_{i=0}^{\lfloor \log_{10} x\rfloor} 9i(10^{i - 1})$$
However, finding the amount of digits of, say, (using equation (1)) $f(6294)$ cannot be done using equation (3). Equation (3) is quite good at finding the number of digits coming before $10^n$, all we need to do is add the digits occurring between $10^n$ and $x$.

$x-10^{\lfloor\log x\rfloor}+1$ gives us the amount of numbers after $10^n - 1$. Taking that number and multiplying it by $\lfloor\log x\rfloor + 1$ gives us the digits after $10^n$. Putting all of that on one line,
$$(4)  (x-10^{\lfloor\log x\rfloor}+1)(\lfloor\log x\rfloor + 1)$$
Using (4) and (3) together, we finally have a way of calculating the number of digits on the interval $[1,x]$.
$$(5) b(x)=(x-10^{\lfloor\log x\rfloor}+1)(\lfloor\log x\rfloor + 1)+T(x)$$

You can now use $b(x)$ to find the amount of digits from 1 to $x$ instantly! What's that you say? You don't need to know that? Hmm, well, we'll label it as a solution looking for a problem. If you have a use for it, let me know in comments, or email me at Aerom.Xundes@gmail.com. Feel free to poke holes in the logic, math, or way it is presented.

No comments:

Post a Comment