5

Find the number of positive integers $$n <9,999,999 $$ for which the sum of the digits in n equals 42.

Can anyone give me any hints on how to solve this?

6 Answers6

7

Let the digits be $d_1,d_2,d_3,d_4,d_5,d_6$, and $d_7$, where we allow leading zeroes so as to make each number in the specified interval a seven-digit integer. You’re looking for all solutions in non-negative integers to

$$d_1+d_2+d_3+d_4+d_5+d_6+d_7=42\;,$$

with the restriction that $d_k\le 9$ for $k=1,\ldots,7$. Without the restriction this is a standard stars-and-bars problem, whose solution is

$$\binom{42+7-1}{7-1}=\binom{48}6\;.\tag{1}$$

Both the formula that I used here and a pretty decent explanation/derivation of it can be found at the link. However, $(1)$ includes unwanted solutions in which one or more of the $d_k$ exceeds $9$. To remove these, you can use an inclusion-exclusion argument. This answer shows such an argument in some detail in a smaller problem of this kind.

Brian M. Scott
  • 616,228
  • 1
    This is a pretty incomplete answer. – yo' Oct 31 '13 at 14:12
  • 1
    @tohecz: On the contrary, when taken with the linked material it does most of the work. In any case it’s not intended to be complete; indeed, it’s intended not to be complete. It’s intended to give the OP enough information to finish solving the problem, while still leaving some work to be done. Many of us prefer not to give complete solutions to problems that are standard undergraduate course material, especially when the OP actually asks for hints. Most people learn more when they do at least some of the work themselves. – Brian M. Scott Oct 31 '13 at 14:21
  • Ok, then I probably miss a concept of "an answer" :-/ Because this is not an answer to the problem :-/ – yo' Oct 31 '13 at 14:27
  • 1
    @tohecz: No, but it’s an answer to the question. I judge each question individually to determine what sort of answer seems most appropriate. Sometimes it’s a complete solution to the problem; often, especially with elementary questions, it’s a push in the right direction, though I tend to give bigger hints than some others. I also try to take into account the level of knowledge of the OP, if there’s any indication of it. – Brian M. Scott Oct 31 '13 at 14:29
5

A solution through generating functions:

Imagine a language of words $\newcommand\L{\mathcal{L}}\L=(a^{\{0,9\}}b)^7$, where $a^{\{0,9\}}$ means "any number of $a$'s betwees $0$ and $9$". A word in this language corresponds to $7$ digits in $0,\dots,9$. The sum of the digits is exactly the number of $a$'s in the word. Therefore we want to compute the number of these words such that they contains $42$ occurences of $a$.

Let $C(x)=\sum_{w\in\L} x^{|w|_a}$, where $|w|_a$ is the number of occurences of $a$ in the word $w$. Then the number of words with $42$ letters is exactly the coefficient of $x^{42}$ in $C(x)$, in other words, it is $\frac{1}{42!}\frac{d^{42}}{dx^{42}} C(x)\Bigr|_{x=0}$.

Now, the regular expression for $\L$ is unambigous (every word is obtained exactly once), therefore to get $C(x)$, we simply erase $b$'s and change $a$'s to $x$'s. We get $\bigl(\sum_{i=0}^9x^i\bigr)^7=\Bigl(\frac{x^{10}-1}{x-1}\Bigr)^7$. If you use Maple and put

eval(diff(((x^10-1)/(x-1))^7, x$42), x=0)/factorial(42);

you get the correct answer 209525.

To generalize this, you "only" need to be able to either get a general formula for the derivatives or to be able to express $C(x)$ in a better way.


EDIT: It actually is possible to obtain the closed formula for this. For our series $C(x)$ we have that $C(x)=\Bigl(\frac{1}{1-x}-x^{10}\frac{1}{1-x}\Bigr)^7=\sum_{i=0}^7 \binom{7}{i}(-1)^i x^{10i} \Bigl(\frac{1}{1-x}\Bigr)^7$. Let $b_n$ denote the coefficient of $x^n$ in $\Bigl(\frac{1}{1-x}\Bigr)^7$. We have $b_n=\binom{n+6}{6}$. Then the coefficient of $x^{42}$ in $C(x)$ is $\binom{7}{0}b_{42}-\binom{7}{1}b_{32}+\binom{7}{2}b_{22}-\binom{7}{3}b_{12}+\binom{7}{4}b_2$. Now you can compute on any calculator (or by hand) that this really equals $209525$.


The general formula for $\ell$ digits summing up to $n$ in the base $b$ is: $$\sum_{i=0}^{\min\{\ell,\lfloor n/b\rfloor\}} (-1)^i \binom{\ell}{i} \binom{n-ib+\ell-1}{\ell-1}.$$

yo'
  • 4,506
  • One can also write the final expression as $$\sum_{br+s=n} (-1)^r \binom{\ell}{r} \binom{\ell + s -1}{s}$$ by writing $r$ for $i$ and $s$ for $n-ib$. – ShreevatsaR Jan 05 '19 at 23:11
5

Consider the product $(1+x+x^2+x^3+\dots+x^9)^7$. Each choice of a term in each factor corresponds to the choice of a digit in a $7$ digit number. The coefficient of $x^{42}$ corresponds to the number of ways for the sum of those digits to be $42$.

Thus, the answer is the coefficient of $x^{42}$ in $$ \left(\frac{1-x^{10}}{1-x}\right)^7 =\left(\sum_{j=0}^7\binom{7}{j}(-1)^jx^{10j}\right)\left(\sum_{k=0}^\infty\binom{k+6}{k}x^k\right) $$ which is given by the Cauchy product of the coefficients of the power series above: $$ \sum_{j=0}^7(-1)^j\binom{7}{j}\binom{48-10j}{42-10j}=209525 $$ where we take $\binom{n}{k}=0$ when $k\lt0$.

robjohn
  • 345,667
  • I see now that this answer is a special case of tohecz more general second answer. I hope the removal of the generality makes this easier to follow, yet still shows how one would extend the answer to other cases. – robjohn Oct 31 '13 at 14:59
  • Well, I mainly wanted to show that generating functions are quite unbelievably powerfull weapon for treating any enumeration problems in combinatorics ;) – yo' Oct 31 '13 at 15:13
  • @tohecz: and that they are :-) – robjohn Oct 31 '13 at 16:08
1

I know this is not the answer you are looking for, but I thought it was an amusing one-liner brute force solution (takes less than 1 minute to run) - and may allow you to verify your own result:

python -c "print len([i for i in xrange(10000000) \
                if sum([int(x) for x in str(i)])==42])"

209525

totaam
  • 111
1

Since $0$ has no weight we can consider all numbers as $7$-place decimals. It follows that we have to count the solutions of $$\sum_{k=1}^7 d_k =42,\qquad 0\leq d_k\leq 9\quad(1\leq k\leq 7)\ .$$ Forgetting about the condition $d_k\leq9$ we have a standard stars-and-bars problem, which has ${42+7-1\choose 7-1}={48\choose6}$ solutions.

Among these solutions there are some with, say, $d_1\geq10$. To count these we count the nonnegative solutions of $\sum_{k=1}^7 d_k=32$ (with the idea to replace in each of these solutions the $d_1$ by $d_1':=d_1+10$ afterwards). There are ${38\choose6}$ such solutions.

This number of "forbidden solutions" has to be multiplied by $7$, since each one of the $d_k$ could be $\geq10$. But in this way we count the cases where two $d_k$s are $\geq10$ twice, and it becomes apparent that we run into an inclusion-exclusion scheme. It follows that the total number $N$ we are looking for is given by $$N={48\choose6}-{7\choose1}{38\choose6}+{7\choose2}{28\choose6}-{7\choose3}{18\choose6}+{7\choose4}{8\choose6}=209525\ .$$ (It is impossible that more than $4$ of the $d_k$ are $\geq10$.)

0

The question is just a variation on this one:

Probability of random integer's digits summing to 12

which provides several neat theoretical solutions.

For computational evaluation, here is a one-liner in Mathematica (takes about 30 seconds on my Mac):

Count[Map[Total, IntegerDigits[Range[9999998]]], 42]

209525

wolfies
  • 5,174