# Find the total number of zeros in given range of decimal numbers

Could anyone please tell me what could be the math function to get the number of zeros in give decimal representation of number? I scratched my head on Combination and Permutation but couldn't come up with generic answer. The number length can be upto 1000s digits so you can represent a number as a String.

For example is numbers range is $1-100$, the answer should be $11$, for $1-200$, it's $20$ and so on! No how would you find total number of zeros between $1-19447494833737292827272\cdots 444$ (any big number)?

Thanks.

-
 oeis.org/A061217 – Charles Nov 10 '11 at 21:36

Say you are writing all $d$ digit numbers, writing leading zeros. Then you would write out $d\times 10^d$ digits total, with each digit occurring exactly $\frac{1}{10}$th of the time, so you would write a total of $d\times 10^{d-1}$ zeros.

So, for example, to write all numbers between $0$ and $99$, writing the numbers less than $10$ as $0d$, you would write $2\times 10^2 = 200$ digits, of which $2\times 10=20$ are zeros.

Of course, of these you don't want to count the leading $0$s of $00-09$, so you subtract $10$; and you don't want to count the second $0$ from $00$, so you subtract another one, giving you a total of $9$ zeros. So you need to adjust the count above.

If you write all numbers of up to $d$ digits, then the number of $0$s, including leading $0$s, is $d\times 10^{d-1}$. Then you subtract the number of leading $0$s that appear in the left-most digit (there are $10^{d-1}$ of them), those that appear in the second-left-most digit when the left-most is $0$ (there are $10^{d-2}$ of them); those that appear in the third left-most digit when the first two are $0$ (there are $10^{d-3}$ of them) and so on. So you get $$d\times 10^{d-1} - (1+10+10^2+\cdots + 10^{d-1}) = \frac{(9(d-1)-1)10^{d-1} + 1}{9}.$$ So, for example, for $d=2$ (from $1$ through $99$), you get $$\frac{(9-1)10^1 + 1}{9} = \frac{81}{9} =9,$$ same as above. For $d=3$, (from $1$ through $999$) you get $$\frac{(9(2) - 1)10^2 + 1}{9} = \frac{1701}{9} = 189.$$

What if you are doing something slightly different, as you write, say, only the numbers between $1$ and $751$?

You can count the zeros from $1$ to $99$ as above.

Then count the number of zeros in numbers of the form $7bx$ with $1\leq b\leq 5$. There's one for each value of $b$, for a total of $5$.

Then count the number of zeros in numbers of the form $70x$; there's $11$ of them.

Then count the number of zeros in numbers of the form $axy$ with $1\leq a\leq 6$; there's $10^{2-1}=10$ of them (you are just counting all zeros in numbers up to 2 digits, counting leading $0$s).

So after counting all the way to $99$, you then add:

1. One zero for each number $7bx$, $1\leq b\leq 5$: total, $5$ (the middle digit of $751$).
2. Zeros for each number $70x$; total, $11$.
3. Zeros for each number $axy$ with $1\leq a\leq 6$; total, $6\times 10^{2-1} = 60$.

There are 9 zeros from $1$ through $99$; and then there are $60+11+5=76$ zeros from $100$ through $751$, for a total of $85$ zeros from $1$ through $751$.

-
OMG! Thank you very much. I understood your logic but wondering how did you get it? I have scratched my head more than two hours but wasn't nearer to figure out what to do when number of digits cross the length 10 as I can't do anything with 10C11 :) – AppleDeveloper Nov 10 '11 at 23:23
@AppleDeveloper: There was a Car Talk puzzler some time ago about counting how many zeros show up in an odometer going from x to y; I did a complicated counting argument; when Ray Mazzoli gave the answer, he mentioned the clever trick of realizing that every digit occurs the same number of times overall and taking the average. That was my starting point here. The really tricky bit is to handle those "partial ranges" when your top number is not of the form $99\cdots999$. – Arturo Magidin Nov 11 '11 at 1:34
Cool. Thank you very much Arturo! – AppleDeveloper Nov 13 '11 at 9:41

The approach is correct, but the answer is wrong. There are 145 zeros from 1 to 751: 9 zeros from 1 to 99, 120 zeros from 100 to 699 (20 x 6) and 16 zeros from 700 to 759

Total: 9+120+16 = 145.

-
 You are right - here is a Mathematica code to check With[{x = 751}, Count[Flatten[IntegerDigits /@ Table[x - n, {n, 0, x - 1}]], 0]] – Norbert Oct 17 '12 at 5:50