[Math] How many positive integers less than 10,000 have at most two different digits

combinatoricsnumber theory

Hi I've been working on this problem, and don't know how to solve it.

Here it is:
How many positive integers less than 10,000 have at most two different digits.

I think this problem has something to do with the inclusion-exclusion principle, but I'm not entirely sure. Thanks!

Best Answer

For simplicity I ignore the digit $0$. You will have to figure that out yourself. But here is the answer to the question "how many positive integer less than $10000$ have at most two different digits and no $0$ in them":

There are $2^4 = 16$ four-digit numbers containing only the digits $1$ and $2$. The same for all other of the $\binom{9}{2}=\frac{9\cdot8}{2} = 36$ combinations of $2$ of the $9$ possible digits. If you add all those together, you will have counted the number "1111" and "2222" and so on too often. In fact you counted them each $8$ times, so you have to substract 7 again. This is called the inclusion-exclusion-principle.

The answer therefore is $36\cdot16 - 9\cdot 7$.