Let me put in this way, its a permutation combination question with following parts
number of zeros from
1-9
10-99
101-999
1000-3999
For the first case the answer is 0
For the second case answer is [1-9]0 so 9
For the third case [1-9]0[0-9] + [1-9][0-9]0 = 9*1*10+9*10*1 = 180
For the third case [1-3]0[0-9][0-9]+[1-3][0-9]0[0-9]+[1-3][0-9][0-9]0 = 300+300+300 = 900
Total: 900+180+9 = 1089
Python Code
def countZeros(n):
result = 0
i = 1
while True:
b, c = divmod(n, i)
a, b = divmod(b, 10)
if a == 0:
return result
if b == 0:
result += (a - 1) * i + c + 1
else:
result += a * i
i *= 10
print countZeros(3999)