Amount of zero in a range

1.9k Views Asked by At

I'm stuck with a calculation. If a person would write down, Everytime he sees a zero and counts +1.

10 counts as 1 zero. 100 counts as 2 zeros. 1000 has 3 zeros. 1005 counts as 2 zeros.

Is there a formula how one is able to calculate how many zeros pass by?

0 - 100 has 12 zeros 100 - 202 has 25 zeros. 0 - 501 has 93 zeros.

At first I was thinking of 100/10 = 10.but the 100 gives 1 extra zero and the zero itself too. With bigger numbers like 100 - 1010 is 10. And 1011 - 120 is only one.

I hope my post is understandable since I'm not a mathematician myself.

edit: numbers can go from any number untill 2^63 as the maximum

example: 0 - 100 = 12 (include the 0)
100 - 201 = 23
0 - 500 = 92
0 - 4294967296 = 3825876150
1234567890 - 2345678901 = 987654304
1

There are 1 best solutions below

5
On BEST ANSWER

I'm not sure if this is what you're looking for, but consider the range of numbers 100-199. There are 100 numbers, but, since each number has a digit in the tens column, and a digit in the ones column, there are actually 200 positions that a zero can occupy. Therefore, there a $200 / 10 = 20$ zeroes in this range of numbers. For the range 1000-1999, there are 3000 positions and therefore 300 zeroes would appear in this range.

Edited here to give a more easy example:

Suppose you want to find the number of zeroes in the range $[1, 7192]$. First, find the number of zeroes in $[1, 7999]$. Following the rationale above, this would be $9 + 9 \cdot 20 + 7 \cdot 300$. Next, we want to subtract out the number of zeroes in $[7193, 7999]$. We start by counting the number of zeroes in $[7200, 7999]$. From the rationale above, this would be $8 \cdot 20$. The number of zeroes in $[7193, 7199]$ is $0$ by inspection. So, the total number of zeroes in the interval $[1, 7192]$ is

$(9 + 9 \cdot 20 + 7 \cdot 300) - (8 \cdot 20) = 9 + 1 \cdot 20 + 7 \cdot 300$

Notice how the digit in the 1000s column is multiplied by 300, the digit in the 100s column is multiplied by 20, and the digit in the 10's column is multiplied by 1. This leads me to think that for a range $[1, z]$, if you express $z$ as

$x_1 + x_2 \cdot 10 + x_3 \cdot 10^2 + \cdots + x_j \cdot 10^{j-1}$

the number of zeroes that appear in the range is

$x_2 + x_3 \cdot 2 \cdot 10 + x_4 \cdot 3 \cdot 10^2 + \cdots$

A number like $4294967296$ would be expressed as:

$6 + (9 \cdot 10) + (2 \cdot 10^2) + (7 \cdot 10^3) + (6 \cdot 10^4) + \cdots$

and the number of zeroes would be:

$9 + (2 \cdot 10) + (7 \cdot 3 \cdot 10^2) + (6 \cdot 4 \cdot 10^3) + \cdots$

To solve the number of zeroes in the range $[1234567890, 2345678901]$, count the number of zeroes in $[1, 1234567890]$ and $[1, 2345678901]$ and subtract the first value from the second.

To understand the rationale why this formula is true for $[1, 7192]$ let $x$ be the number of zeroes in the range $[1, 999]$, $y$ be the number of zeroes in the range $[1000, 7999]$ and $z$ be the number of zeroes in the range $[7193, 7999]$. Then, the number of zeroes in the range $[1, 7192]$ is $x + y - z$. But, the number of zeroes in the range $[7193, 7999]$ is equal to the number of zeroes in the range $[193, 999]$. So we can also count the number of zeroes by counting the zeroes in the ranges $[1,192]$ and $[1000, 7999]$. You can then make the same sort of argument for $[1, 192]$ and use recursion to show the validity of the formula. This idea could be used to provide a more formal proof.