Given a list of integers 1 ⋯ n , return the count of the number that have the number k in them. For n ≤ 1 0 9 how many numbers have the number 7 in them.
Details and Assumptions
For n ≤ 1 0 , the number of occurrence of 7 is one.
For n ≤ 1 0 0 the number of occurrence of 7 is 1 9 ... 7 , 1 7 , 2 7 , 3 7 , 4 7 , 5 7 , 6 7 , 7 0 , 7 1 ⋯ , 7 9 , 8 7 , 9 7
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.
Do you see a more direct way to explain why the answer is 1 0 n − 9 n ?
Hint: Complement
There are also two other ways to see this result. They are both combinatorial approaches:
Numbers upto 1 0 n can be seen as numbers of the form a 1 a 2 … a n where 0 ≤ a i ≤ 9 ∀ 1 ≤ i ≤ n
Approach 1: We fix r of the a i 's as 7 which can be done in ( r n ) ways and the ( n − r ) a i 's can be any digit other than 7 , so there are 9 choices for each of those digits. So, the number of such numbers is r = 1 ∑ n ( r n ) 9 n − r = − 9 n + r = 0 ∑ n ( r n ) 9 n − r = − 9 n + 1 0 n = 1 0 n − 9 n where the sum was simplified using the binomial theorem.
Approach 2: We consider the complement of the required event. Numbers that don't have 7 in them have a digit other than 7 in all the a i 's, so there are 9 choices for each a i and hence there are 9 n numbers upto 1 0 n which don't have 7 in them. There are a total of 1 0 n positive integers upto 1 0 n , so we subtract 9 n from 1 0 n to get the number of numbers that don't have 7 in them, so the required number of numbers is 1 0 n − 9 n .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Output:
1 |
|
We certainly know that 10^9 doesn't contain a 7 in it. All the numbers preceding it can be represented using 9 digits. For e.g. 9 can be represented as 000000009 . So the total numbers you would be able to create would be 10^9. We know have to subtract the numbers which don't contain 7 from this value. Therefore our answer should be 10^9 -9^9=612579511
Let f ( k ) be the number of integers with the digit 7 of 1 ≤ n ≤ 1 0 k , then we have f ( k + 1 ) = 9 f ( k ) + 1 0 k . For k = 9 , it is easily calculated with an Excel spreadsheet as follows:
The answer is 6 1 2 5 7 9 5 1 1
We have a 9 digits in the number, x1 10^9+x2 10^8+...+x9*10^0
Lets say, that c is position of last 7 (x[c] = 7) in this number, so we can insert any digit before it, and can`t insert 7 after. Number of variants will be 10^(c-1)*9^(9-c).
Answer for task is the sum of variants for all c (positions of last 7).
sum( 9^(9-c) * 10^(c-1), c = 1...9 )
Problem Loading...
Note Loading...
Set Loading...
For n ≤ 1 0 0 we had 19 occurrences. Now for n ≤ 1 0 0 0 we will repeat these 19 occurences 9 times, actually 10 times but we don't want to double count them for 7xx. There are 100 numbers of the form 7xx. In total we get 9*19 + 100 = 271.
a ( n ) = 9 ∗ a ( n − 1 ) + 1 0 n − 1
Or in a non-recursive form:
a ( n ) = 1 0 n − 9 n