How many 6-digit numbers have a sum of digits of 8? Note that the first digit cannot be zero. That is a restriction. The first digit must be at least 1. That takes away one of the "8" that we have freedom to distribute among the digits. It's as if $8 needs to be deposited into 6 accounts, but the first account must get at least $1. Therfore, we have the freedom to distribute the other $7. So, in formulating this as a ball-in-urn problem, we have 7 identical objects, and 6 categories being the digits themselves. Six categories means 5 dividers. We model this situation as a binary number having 7 zeros and 5 ones, for a total of 12 bits. How many such binary strings could be created? It is C(12, 5). -------------------------------------------------------------------------- How many 6-digit numbers have a sum of digits of 13? Again, we have the restriction that the first digit has to be at least 1. But we have a second restriction that no digit may be 10 or higher. Let's count the number of "illegal" numbers and subtract from a gross total. A word of caution! There is overlap between our two restrictions. When we exclude illegal numbers that start with the digit 0, we are also eliminating some numbers that have 10+ in some other digit place. For example, the illegal number with 0 as the first digit and 11 as the second digit satisfies both restrictions. We should not eliminate this kind of number twice! How many 6-digit numbers would there be ignoring all restrictions? We would have 13 identical objects and 6 categories, meaning 5 dividers. We model this as 13 zeros and 5 ones, which is an 18-bit string. How many 18-bit strings have 5 ones? It is C(18, 5). Let's look at the first restriction. Let's demand that the first digit be zero. Essentially, this means that we only want a 5-digit number. So, we have 13 identical objects and 5 categories (hence 4 dividers). This is like having 13 zeros and 4 ones, a 17-bit number. We want how many 17-bit numbers have 4 ones. It is C(17, 4). Let's look at the 2nd restriction. Let's demand that some digit be 10 or higher. Because the sum of digits is 13, we only have enough "money" to cause one digit to overflow. But any of the 6 digits may overflow. For example, the first digit might overflow. We would want the first digit to be 10 or higher. Since 13-10 = 3, we only have the freedom to allocate the other 3 "dollars". We have 3 identical objects and 6 categories: 5 dividers. This reminds us of a binary string with 3 zeros and 5 ones. C(8, 3). But any one of the 6 digits may overflow, so this gives us: 6 C(8, 3). Finally, let's consider the combination of the 2 restrictions. We want the first digit to be 0, AND one of the other 5 digits to be 10 or higher. This is like saying we want a 5-digit number to have one of its digits overflow. So, we handle this like we did with the 2nd restriction alone, but for 5 digits. We have 3 identical objects and 5 categories: 4 dividers. This becomes a 7-bit number with 3 zeros and 4 ones. C(7, 3). But any one of the 5 digits could overflow, so we have: 5 C(7, 3). Our final answer is: gross number - 1st restriction - 2nd restriction + combined restriction = C(18, 5) - C(17, 4) - 6 C(8, 3) + 5 C(7, 3). To verify this answer, we can write a computer program to literally count how many such numbers exist. I checked, and the answers agree: 6027. -------------------------------------------------------------------------- Alternative solution, with a somewhat more efficient approach: We have 13 identical balls to put into 6 urns. But the first urn must contain at least one ball. This is because in order to have a 6-digit number, the first digit must be 1, or else it is not a 6-digit number. So, we can freely distribute 12 balls among 6 containers. Thus, there are 5 dividers and so the gross number of possibilities is C(17, 5). However, because we are talking about digits of a number, we are not allowed to have any digit value higher than 9. So, we must subtract the impossible cases. There is an impossible case for each digit. An impossible case means that one digit must have a value of 10 or higher. Don’t forget we still have the restriction that the first digit must be at least 1. Impossible case for first digit: first digit is at least 10. Then we have 13 – 10 = 3 balls to freely distribute among 6 urns. This is C(8, 3). Impossible case for the other digits. There are 5 identical cases. In each case, the first digit is at least 1, and some other digit is at least 10. Then we have 13 – 1 – 10 = 2 balls to freely distribute among 6 urns. This is C(7, 2). Multiply this by 5 because there are 5 such cases. Our final answer is C(17, 5) – C(8, 3) – 5 C(7, 2). -------------------------------------------------------------------------- In the long run, questions involving sums of digits are probably more straightforward to solve using the generating function technique, which we will study next.