How many '1's appear in the following number:
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 . . . 9 9 9 9 9 8 9 9 9 9 9 9 1 0 0 0 0 0 0 ?
It is formed by joining the integers from 1 to 1000000 back-to-back and in order. You should be able to see the ? question mark at the back of the number.
This problem is part of the set Easy Problems
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.
I found the metaphor of one-armed bandit gambling machine useful here.
Consider numbers from 0 to 999999. We can as well add leading 0 to any number below 100000, since it has no effect on the number of 1's in the original string from problem statement. This is handy since we can now let the one-armed bandit have 5 drums numbered from 5 (leftmost) to 1 (rightmost), each containing digits from 0 to 9.
Now, if we know how many 1's are contributed by drum 1, drum 2, etc., and not mixing counts from different drums, the total number of 1's would be of form:
N ( 1 ) ∗ M ( 1 ) + N ( 2 ) ∗ M ( 2 ) + . . . + N ( 5 ) ∗ M ( 5 )
where
Getting at N ( i ) :
There is 1 0 6 numbers from 0 to 999999. There must be 1 0 6 / 1 0 i number of revolutions of drum i to count to the last number. It can be verified for smaller magnitudes, e.g. 1 0 2 / 1 0 1 for numbers 0 to 99.
Given that, N ( i ) = 1 0 6 / 1 0 i
Finding M ( i ) :
For i=1, given that we agreed to count 1's on all drums separately, one revolution can only yield one 1's.
For i=2, there are two cases:
There's 10 turns of 1st drum when 2nd shows "1".
For i=3
There's 100 turns of 1st drum when 3rd shows "1".
etc..
Bringing all together
1 0 6 / 1 0 1 ∗ 1 + 1 0 6 / 1 2 ∗ 1 0 + 1 0 6 / 1 0 3 ∗ 1 0 0 + . . . + 1 0 6 / 1 0 6 ∗ 1 0 0 0 0 0 = 6 0 0 0 0 0
Since we've not included the 1000000 and the single 1 it provides in our consideration: 6 0 0 0 0 0 0 + 1 = 6 0 0 0 0 0 0 .
The answer is 6 0 0 0 0 0 0 .
Problem Loading...
Note Loading...
Set Loading...
Consider numbers from 0 to 999999. At each place, probability of every digit is equal i.e. 1 0 1 . So at each place, "one" will appear 1 0 1 0 6 = 1 0 5 times. Therefore at 6 places, it will appear 600000 times. In the last number 1000000, it will appear 6 0 0 0 0 1 t h time!!
I know its a bit messy! Hope clean enough to understand!