Counting Zeroes

Let N N be the number of 0 0 digits in all the integers from 0 0 to M M (including the initial 0 0 ). Find the [smallest] non-trivial value of M > 1 M > 1 where N = M N=M .

For example, for M = 100 M=100 , N = 12 N=12 (because it includes the initial 0), hence N M N≠M , so that M = 100 M=100 wouldn't be the answer. There is [not] exactly one non-trivial M M for which this is true, other than the trivial case M = 1 M=1 .

You may use the computer or Wolfram Alpha.

Start counting the zeroes from 0 0 , not 1 1 , otherwise your program (if you're using one) will be a good example of Turing's Halting Problem.

{Edit, problem correction. Others have alerted me of the existence of at least 2 other M, both larger than the one stated as the solution, that meets the condition. I stand corrected, and the problem has been re-worded to ask for the smallest such non-trivial M. I think this problem now has just become more interesting, thanks to the contributors.]


The answer is 100559404365.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Russel Simmons
Jul 8, 2014

My strategy for counting zeroes in a given number was the same as in Joanne's solution, so I won't repeat that here. My full Python code is below. It first establishes upper and lower bounds within which the solution must lie, and then bisects to find the exact solution. It runs instantaneously on my computer.

def zeroes(n):
    '''calculates count of 0 digits appearing in all integers [0..n]'''
    count = 1
    s = str(n)
    factor = 1
    for split in range(1, len(s)):
        front = s[:-split]
        back = s[-split:]

        count += factor*(int(front) - 1)
        count += min(int(back)+1, factor)

        factor *= 10
    return count

# establish bounds
lower = 1
upper = 2
while zeroes(upper) < upper:
    lower = upper
    upper *= 2

print 'must be between %d and %d' % (lower, upper)

# bisect to find solution
while True:
    middle = (lower + upper) / 2

    zm = zeroes(middle)
    if zm < middle:
        lower = middle
    elif zm > middle:
        upper = middle
    else:
        assert zm == middle
        print 'SOLVED', middle
        break

would you please tell me what was the running time of the program?

Ujjwal Kataria - 6 years, 11 months ago
Chew-Seong Cheong
Jul 13, 2014

This is an interesting question. I have tried to solve it with a computer program but my program got time overrun, indicating that my programming sucks. I then came up with an analytic way of solving it. The program proves that the following equation I have come up with is correct.

N = 1 + i = 1 n 1 10 i 1 M 10 i i = 1 n 1 u ( d i = 0 ) 10 i ( 1 M 10 i ) N\quad =\quad 1+\sum _{ i=1 }^{ n-1 }{ { 10 }^{ i-1 } } \left\lfloor \frac { M }{ { 10 }^{ i } } \right\rfloor -\sum _{ i=1 }^{ n-1 }{ u\left( { d }_{ i }=0 \right) } { 10 }^{ i }\left( 1-\left\lceil \frac { M }{ { 10 }^{ i } } \right\rceil \right)

where

  • M M and N N mean the same in the question

  • n = n= the number of digits of M M

  • i = 0 , 1 , 2... , n 1 i = 0, 1,2...,n-1 , the i t h i^{th} position of a digit of M M , 0 0 being the last

  • d i = d_{i}= the value of the i t h i^{th} digit of M M

  • x = \left\lfloor x \right\rfloor = the largest integer smaller than or equal to x x

  • x = \left\lceil x \right\rceil = the decimal residual of x x

  • u ( x = 0 ) = { 1 , i f x = 0 0 , i f x 0 u\left( x=0 \right) =\begin{cases} 1,\quad if\quad x=0 \\ 0,\quad if\quad x\neq 0\quad \end{cases}

  1. The first term 1 1 is for the case when M = 0 M=0 .
  2. The second term is for the fact that for every 10 10 of M M , there is 1 zero; every 100 100 , 10 zeros; every 1 0 i 10^{i} , 1 0 i 1 10^{i-1} zeros.
  3. Statement 2 is not true if d i = 0 d_{i}=0 , the third term is to less out the 1 0 i 10^{i} zeros and add back the zeros due to the remainder (number right of d i + 1 d_{i} + 1 ).

I manually iterated using the equation with a spreadsheet to obtain the answer 100559404365 \boxed{100559404365} .

Joanne Lee
Jun 19, 2014

Let's look at a random number (40321) and see if we can come up with a formula to calculate N.

To count in an organized fashion, we'll count all the values less than or equal to N that have a zero in the ones digit, then add that to all the numbers with a zero in the tens digit, and etc.

We'll first look at the numbers with a zero in its ones digit. Excluding the case when M=0 (we'll add that in the end), we know that there are 4032 numbers with a zero in the ones digit because the first four digits of the number could be anything between 1 and 4032.

Moving on to the tens digit, we can do something similar. The first three digits could be in one of 403 ways, the tens digit must be 0, and it doesn't matter what the ones digit is. Therefore, there are 403 * 10 = 4030 numbers with a zero in the tens place.

The same could be said for the hundreds place. The first two digits could be in one of 40 ways, and it doesn't matter what the last two digits are. Therefore, there are 40 * 100 = 4000 numbers with a zero in the hundreds place.

Now the thousands place is a bit different. If the thousands place is a zero, there are two different cases we must account for. If the ten thousands place is 1, 2, or 3, the last three digits can be anything, so there are 3 * 1000 = 3000 numbers that have a zero in the thousands digit. If the ten thousands place is 4, only 322 numbers have a zero in the thousands place (40000 to 40321). Therefore, there is a total of 3000 + 322 = 3322 numbers with a zero in the thousands place.

We know that there can't be a zero in the ten thousands place, so there we are done. We sum these numbers up, including 0: 4032 + 4030 + 4000 + 3322 + 1 = 15385 zeros that appear.

I decided to program this in Mathematica because Mathematica likes numbers. It took a while to run, but the answer does work.

x = 100559404365;
Sum[
  If[
   Mod[Floor[x/10^(i - 1)], 10] == 0, 
   (Floor[(x - 10^i)/10^i])*10^(i - 1) + Mod[x, 10^(i - 1)] + 1,
   Floor[x/10^i]*10^(i - 1)
   ],
  {i, Total@DigitCount[x]}] + 1

Basically, what we have found is that for the i i th digit from the right, if the digit is not equal to zero, the number of zeros that will occur on that particular digit is equal to

Floor[x/10^i]*10^(i - 1)

For the digits that are 0, the number of zeros that will occur is equal to

(Floor[(x - 10^i)/10^i])*10^(i - 1) + Mod[x, 10^(i - 1)] + 1

We just iterate this over all the digits and sum all the zeros and tada~ We're done.

Impressive solution, Lee. Thanks for validating my problem. I had to throw in that extra 0 as the "first integer" just to make this work out, after pulling out some of my hair. Otherwise, it turns out, there is no such number N.

Michael Mendrin - 6 years, 11 months ago

Log in to reply

Damn it. I misread the question and calculated starting from that 1 1 . I was using a similar technique. I even made a code. But it just never stopped running.

Siam Habib - 6 years, 11 months ago

Log in to reply

Siam, I'll try to make that clearer in the problem statement.

Michael Mendrin - 6 years, 11 months ago

Log in to reply

@Michael Mendrin Thanks! By the way, this was one of the most interesting problem that I saw through out the whole day.

Siam Habib - 6 years, 11 months ago

Log in to reply

@Siam Habib A very interesting problem indeed. I'm embarrassed to admit I wrote brute-force code that ran for about 8 hours.

Nice solution.

Steven Perkins - 6 years, 11 months ago

Michael,

For the submitted dispute, the answers that were submitted are 107374179 and 536870895 (neither of which are equal to yours).

The reasoning that was given was: i have created a code using c...for the above question..

include<stdio.h>

int main() { long int c=1,m,n,i,j; for(i=1;;i++) { j=i; if(i%10==0) { while((i%10==0)&&(i!=0)) { c++; i=i/10; }

             }
             i=j;
            if((c==j))
            printf("%d\n",c);
}

}

on execution ..it reveals that the question has more than 1 correct answers

If sounds like you didn't receive the explanation as stated above. If so, could you forward the email that you received to Calvin@Brilliant.org, so that I can figure out what went wrong? Thanks!

Calvin Lin Staff - 6 years, 11 months ago

Log in to reply

That code has an integer overflow problem. It is written to never end, but seems to be using 32-bit integers. The integers keep wrapping, the cnt keeps incrementing and eventually 2 bogus solutions are found.

The count of zeros for 107374179 is actually 81753408 for example.

Michael gave what I believe is the unique solution.

Steven Perkins - 6 years, 11 months ago

Calvin, I'm little surprised with the larger figure, that seems counter intuitive in light of the behavior of the function counting zeros, although I should have checked for any "nearby solution". I'll amend the wording of the question right now to ask for the "smallest" M. Maybe the more challenging question is "how many such M are there?"

Michael Mendrin - 6 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...