[Math] How many numbers between $1$ and $9999$ have sum of their digits equal to $8$? $16$

combinatorics

How many numbers between $1$ and $9999$ have sum of their digits equal to $8$? $16$?
Can someone tell me if I got the right answers? I solved both cases and I've got $148$ for $8$ and $633$ for $16$. I solved this problem using $x_1+x_2+x_3+x_4=8$ then $16$. After that I decided to take the case when $1$ number is bigger than $10$, but I'm not sure if that's the way I should do it for $8$. And then I substract the $4$ cases when $x_1=0$ and $7$ cases when $x_1=9$. Thank you.

Best Answer

You're half-correct. By stars and bars, the number of solutions to $$x_1+x_2+x_3+x_4=8$$ is exactly $\binom{8+4-1}{4-1}=165$. I don't understand however why numbers like $8=0008$ don't count, since, as Thomas Andrews pointed out in the comments, the sum of its digits add up to $8$.


However, for $16$, our approach cannot be exactly the same, since a solution to $x_1+x_2+x_3+x_4=16$ could be $(x_1,x_2,x_3,x_4)=(1,2,3,10)$, but $10$ cannot be a digit, as those are always between $0$ and $9$. Since only one can be $10$ or larger, we can simply count the number of solutions where one is $10,11,\cdots,16$. If one is $10$, then we count the number of solutions to $x_1+x_2+x_3=16-10=6$. Use stars and bars again and we get $\binom{6+3-1}{3-1}$. Now we multiply by $4$ since we now chose $x_4=10$ but we also need to count $x_1,x_2,x_3=10$. So we get $4\binom{6+3-1}{3-1}$. Using this principle we get $$4\left(\binom{6+3-1}{3-1}+\binom{5+3-1}{3-1}+\cdots+\binom{0+3-1}{3-1}\right)=4\cdot84=336$$ The total amount of solutions to $x_1+x_2+x_3+x_4=16$, including those we don't want, are (again, stars and bars) $\binom{16+4-1}{4-1}=969$. Now substract the amount of solutions we don't want, and we get $969-336=633$ possibilities.

Related Question