How many times does '13' appear in a 6 digit number

I'm trying to find the number of times that '13' appears in a 6 digit number (meaning from 100,000 to 999,999). This is how I'm trying to solve this problem.
There's 5 different cases I'm looking at:

  • Case A) 13_ _ _ _
  • Case B) _ 13 _ _ _
  • Case C) _ _ 13 _ _
  • Case D) _ _ _ 13 _
  • Case E) _ _ _ _ 13

Case A) There's 4 digits, each which can take any number from 0 to 9. Therefore I get 10 x 10 x 10 x 10 = 10,000

Case B) The first digit can take any number from 1 to 9 (I can't use 0 since that would make my number only 5 digits long). For the last 3 digits, it can be any numbers from 0 to 9. I thus get 9 x 10 x 10 x 10 = 9,000

Case C) Similar as Case B, I get 9 x 10 x 10 x 10 = 9000. I know this is including duplicates from Case A (the duplicates being any values that go in 1313 _ _ )
I can calculate that the number of duplicates is 10 x 10 = 100 which will be subtracted at the end .

Case D) Similar to Case C, 9 x 10 x 10 x 10 = 9000. This includes duplicates from both case A (13 _ 13_) and case B (_ 1313_ ). Respectively, the duplicates would be 10 x 10 and 9 x 10.

Case E) Same as before, I get 9 x 10 x 10 x 10 = 9000. This includes duplicates from all cases though. For Case A, the duplicates would be 10 x 10. Duplicates for Case B would be 9 x 10. Duplicates for Case C would be 9 x 10. And there's an additional case where there's duplicates from Cases A and B (where you have 131313), therefore that's 1 I need to subtract at the end too.

My problem/question is, how can I approach this problem without having to subtract duplicates that way? It's confusing and the issues is I need to repeat this problem but with a 7 digit number, then an 8 digit number, etc etc. Is there a better method to calculate the number of possibilities for each case while simultaneously accounting for duplicates?


All digits except the leading digit must be in the range $\left[0,9\right]$, but the leading digit must be in the range $\left[1,9\right]$, this is why the number of $n$-digit numbers is $9\times10^{n-1}$. So if you're looking for a $k$-digit sequence that falls within an $n$-digit number (for the sequence "13", $n=2$)

Initially, let's exclude numbers where the desired sequence covers the leading digit (Case A). This means that the leading digit would always be free to range from 1 to 9, and all other digits, except those covered by the sequence would be free to range from 0 to 9. If we start with $n$ digits, eliminate the $k$ digits in the sequence, and also eliminate the one leading digit, this leaves us with $n-k-1$ digits that can range from 0 to 9.

Thus, there are $10^{n-k-1}$ possibilities for each position, and $n-k$ positions.

Accounting for the leading digit's 9 possible values gives $9\times10^{n-k-1}\times\left(n-k\right)$ possibilities.

Now, if the leading digit of the sequence happens to be $0$, then it can't include the leading digit of the larger number.

For example:

  • _ 0 1 3 _ _
  • _ _ 0 1 3 _
  • _ _ _ 0 1 3

Here, 0 1 3 _ _ _ is not a valid six-digit number. If that's the case we're done, and the answer is $9\times10^{n-k-1}\times\left(n-k\right)$. However, if the leading digit of the sequence we're looking for is nonzero, then we also have to include the cases where it's in the leftmost position. Here, we only have to worry about the $n-k$ digits in the full range from 0 to 9, for an additional $10^{n-k}$ possibilities.

So the total number of times a $k$-digit decimal sequence will appear in an $n$-digit number is:

$$9\times10^{n-k-1}\times\left(n-k\right)+\begin{cases}0&\text{if leading digit of sequence}=0\\10^{n-k}&\text{otherwise}\end{cases}$$

That's the total number of times the sequence will appear. However, as @JMoravitz pointed out, if you care about how many $n$-digit numbers there are in which it appears, then you also have to take into account whether it can overlap itself. That is, if the first $p$ digits of this $k$-digit sequence are the same as the last $p$ digits, then it could appear up to $\left\lfloor\frac{n-k+p}{k-p}\right\rfloor$ times in the same $n$-digit number, and you'll need to account for that as well.