# How many N-digit numbers have the sum of odd places digits equal the sum of even places digits?

We define a N-digit number as a sequence $$d_{1}d_{2}d_3...d_N$$, where $$d_k\in\{0,1,2,3,4,5,6,7,8,9\}$$. So the first digits can be equal to zero. N is a positive even integer. The problem is to find, how many N-digit numbers are there, which have their sum of the digits on odd places equal to the sum of digits on the even places. Example number would be 2563 for 4-digit numbers as 2 + 6 = 5 + 3.

My thought was to choose the odd digits, we can do that in $$10^{N/2}$$ ways and then for each of that combinations make even digits a permutation of them ($$\frac{N}{2}!$$), but this, if correct at all, is only a small fraction of all the possibilities. Why? Because the digits do not have to be the same. Only the sums have to be equal.

Then my second though was that each of such numbers consists of digits from other (shorter) numbers that satisfy the requirement, but it is not true since only the sums have to be equal.

We can for each sum of odd/even digits calculate how many ways we can express it on N/2 digits and then sum the squares of such numbers of combinations. However that leads to very unfriendly calculations.

After you find a nice way, please provide some example calculations for N=4 and N=6.

• sum of digits in odd places = sum of digits in even places $\iff$ $N$ is divisible by $11$ Jan 3, 2021 at 20:25
• @J. W. Tanner I fear this is not the only requirement. Jan 3, 2021 at 20:54
• I did some research, it only works one way. e.g. 0605 is 121*5 Jan 3, 2021 at 21:01
• @Kangaroo976 I have given a hint in the answers. That will give you an expression but I am curious about your edit: The second part of what? Why do you want/hope for stirling numbers to appear? Jan 3, 2021 at 21:14
• @Phicar Your hint makes sense, but in this case it's like brute forcing it. The task is from an archival local math contest. And it has two parts, first to find a formula, then the second to calculate for N=4 and N=6. They provide some definitions upfront, that year it was Stirling and Catalan numbers. Jan 4, 2021 at 8:14

The problem is considered in the series of papers here (in Russian), where the required number $$C_N$$ is called the number of $$N$$-digital lucky tickets. It is known that

$$C_N=\frac 1{\pi}\int_0^\pi \left(\frac {\sin 10 x}{\sin x}\right)^N dx\approx \frac{10^N}{\sqrt{33\pi n}}.\label{1}\tag{1}$$ The relative error of the approximation is no more than $$4\%$$ and quickly decreases when $$N$$ grows.

For small $$N$$ in the second paper are provided calculations of $$C_N=\sum _{k=0}^{9N}\mathcal{A}_k^2$$, mentioned by Phicar. Namely, for given $$n$$, $$\mathcal{A}_k$$ is denoted by $$N_n(k)$$, where $$n=N/2$$ and is provided a recurrent formula

$$N_n(k)=\sum_{l=0}^9 N_{n-1}(k-l), \label{2}\tag{2}$$ $$\begin{matrix} \mbox{where if k<9 then N_{n-1}(k-l)=0 for l>k;}\\ \mbox{N_1(k) equals 1, if 0\le k\le 9 and equals 0, otherwise.} \end{matrix}\label{3}\tag{3}$$

The recurrence allows to build a table for $$N_n(k)$$ for $$n\le 4$$ and then calculate $$C_2=10$$, $$C_4= 670$$, $$C_6=55 252$$, and $$C_8=4 816 030$$.

Moreover, following the observations from the second and third papers, we easily see that a map $$d_{1}d_{2}d_3\dots d_N\mapsto d_1(9-d_2)d_3(9-d_4)\dots d_{N-1}(9-d_N)$$ is a bijection between the set of $$N$$-digit numbers which have their sum of the digits on odd places equal to the sum of digits on the even places and a set of $$N$$-digit numbers with the sum of the digits $$9N/2$$. That is, the size $$C_N$$ of the first set equals the size $$N_{N}(9n)$$ of the second set.

In the tiny third paper by means of generating functions is shown that $$N_n(k)=C_{10}(n,k)=\sum_{i=0}^{\min\{n,k/10\}} (-1)^i {n\choose i}{n+k-10i-1\choose n-1}.$$

As an illustration, there is shown that $$C_6=N_6(27)=C_{10}(6,27)= \sum_{i=0}^{2} (-1)^i {6\choose i}{32-10i\choose 5}=$$ $${32\choose 5}-{6\choose 1}{22\choose 5}+{6\choose 2}{12\choose 5}=55 252.$$

I am reading the papers and updating the answer.

• Nice answer. Expecting an interesting derivation ... (+1) Jan 13, 2021 at 19:49

Hint: The key to solve this is to know what the digits add up to say a number $$k$$. Iterate over it and use stars and bars with inclusion exclusion to get the number of compositions of $$k$$ where the parts of the compositions are bounded by $$9.$$ Call this number $$\mathcal{A}_k,$$ then the answer is $$\sum _{k=0}^{9\cdot N}\mathcal{A}_k^2.$$ To compute $$\mathcal{A}_k$$ notice first that if we allow "digits" greater than $$9$$ then it would be $$\mathcal{B}_k=\binom{k+N/2-1}{k}.$$ So consider set $$B_{k,i}$$ where the $$i-th$$ digit is greater than $$9$$ then you want to compute $$\left |\mathcal{B}_k\setminus \bigcup _{i=1}^{N/2}B_{k,i}\right |.$$ Use the principle there to conclude with a not very nice formula.

Note: This is just a supplement to the answer from @AlexRavsky which I think is a most convenient approach. For those who are interested in a nice derivation of the problem written in English may consult section 1.1 The lucky tickets problem in*Lectures on Generating Functions* by S.K.Lando.

The author introcudes this section by mentioning that in the early 70s A.A. Kirillov usually opened his seminar with this problem :-)

Here is a variation based upon PIE the principle of inclusion-exclusion. We consider digits in $$\mathcal{V}=\{0,1,\ldots,9\}$$ and words of even length $$N=2m$$ built from $$\mathcal{V}$$. We are looking for the number $$a_N$$ of words where the sum of digits at even positions is equal the sum of digits at odd positions: \begin{align*} d_1d_2\ldots d_{N-1}d_N\qquad\mathrm{with}\qquad\sum_{j=1}^{m} d_{2j}=\sum_{j=1}^{m} d_{2j-1}\tag{1} \end{align*} We replace the digits $$d_{2j}$$ at even positions with $$9-d_{2j}$$ and consider \begin{align*} d_1\left(9-d_2\right)\ldots d_{N-1}\left(9-d_N\right)\qquad\mathrm{with}\qquad\sum_{j=1}^{2m} d_{j}=9m\tag{2} \end{align*} We have an obvious bijection between the words in (1) and (2). We can consequently calculate $$a_N$$ by counting the number of words with $$N$$ digits from $$\mathcal{V}$$ which have sum $$9m$$.

PIE: We apply PIE as follows:

• We consider the set of all distributions of non-negative integers at $$N=2m$$ positions which have sum $$9m$$. This number is given by \begin{align*} \binom{9m+2m-1}{2m-1}=\binom{11m-1}{2m-1} \end{align*}

• We denote with the property $$p_j, 1\leq j\leq N$$ those words where the number at the $$j$$-th position is at least $$10$$. The number of words with length $$N=2m$$ which have property $$p_j$$ is \begin{align*} \binom{11m-11}{2m-1} \end{align*}

• The number of words which have for $$1\leq j\ne k\leq N$$ at positions $$j$$ and $$k$$ at least $$10$$, which is the number of words which have properties $$p_{j}$$ and $$p_k$$ is \begin{align*} \binom{11m-21}{2m-1} \end{align*} Continuing this way we observe the wanted number $$a_N$$ is \begin{align*} \color{blue}{a_N}&\color{blue}{=\binom{11m-1}{2m-1}-\binom{N}{1}\binom{11m-11}{2m-1} +\binom{N}{2}\binom{11m-21}{2m-2}-+\cdots} \end{align*}

Example: $$N=2m=6$$

We obtain: \begin{align*} \color{blue}{a_6}&=\binom{32}{5}-\binom{6}{1}\binom{22}{5}+\binom{6}{2}\binom{12}{5}\\ &=201\,376-6\cdot 26\,334+15\cdot 792\\ &\,\,\color{blue}{=55\,252} \end{align*}

This answer is intended to be simple and self-contained but please ask if it has not been expressed sufficiently clearly.

The expansion of $$(1+x+x^2+...+x^9)^n$$ as $$\sum a_ix^i$$

Consider multiplying out $$(1+x+x^2+...+x^9)(1+x+x^2+...+x^9)...(1+x+x^2+...+x^9).$$

For a given integer $$M$$, a term $$a_Mx^M$$ in the product is given by the sum of terms $$x^ux^v...$$ where $$x^u$$ is from the first bracket, $$x^v$$ is from the second bracket and so on. Thus $$a_M$$ is precisely the number of ways of expressing the number $$M$$ as the sum of $$n$$ digits.

The sum of squares

We require $$\sum a_M^2$$. This is the constant coefficient in the product $$\sum a_Mx^M\sum a_Mx^{-M}$$ or, equivalently, the coefficient of $$x^{9n}$$ in $$(1+x+x^2+...+x^9)^{2n}$$

The calculation for $$N=4$$ i.e. n=2

We require the coefficient of $$x^{18}$$ in $$(1+x+x^2+...+x^9)^{4}=(1-x^{10})^4(1-x)^{-4}$$.

Expanding, we obtain

$$(1-4x^{10}+...)(1+4x+...+\begin{pmatrix}11\\8\\\end{pmatrix}x^{8}+...+\begin{pmatrix}21\\18\\\end{pmatrix}x^{18}+...)$$ The required number is $$\begin{pmatrix}21\\18\\\end{pmatrix}-4\begin{pmatrix}11\\8\\\end{pmatrix}=670.$$

The calculation for $$N=6$$ i.e. n=3

We now require the coefficient of $$x^{27}$$ in $$(1-x^{10})^6(1-x)^{-6}$$. This is $$(1-6x^{10}+15x^{20}-...)(1...+\begin{pmatrix}12\\7\\\end{pmatrix}x^{7}+...+\begin{pmatrix}22\\17\\\end{pmatrix}x^{17}+...\begin{pmatrix}32\\27\\\end{pmatrix}x^{27}+...)$$ The required number is $$\begin{pmatrix}32\\27\\\end{pmatrix}-6\begin{pmatrix}22\\17\\\end{pmatrix}+15\begin{pmatrix}12\\7\\\end{pmatrix}=55252.$$

A general formula

Let $$K=9N/2$$. Then the calculation we require is just $$\begin{pmatrix}K+N-1\\K\\\end{pmatrix}-\begin{pmatrix}N\\1\\\end{pmatrix}\begin{pmatrix}K+N-11\\K-10\\\end{pmatrix}+\begin{pmatrix}N\\2\\\end{pmatrix}\begin{pmatrix}K+N-21\\K-20\\\end{pmatrix}-....$$

This is a separate answer to note that the cases $$N=4$$ and $$6$$ can be solved very easily by a geometrical method, although this method cannot be extended very easily.

The case N=4

If you consider the two dimensional possibility space with $$0$$ to $$9$$ on each axis, then the number of choices for each sum is given by counting vertices on a diagonal of the form $$x+y=c$$.

These numbers are $$1,2,...,9,10,9,...,2,1$$ and the sum of squares is $$\frac{1}{3}.9.10.19+100=670$$.

The case N=6

The possibility space is three dimensional and we have to consider vertices on planes $$x+y+z=c$$. Most of these planes intersect the possibility space in a triangular shape and the numbers of vertices on these planes are two sets of the triangular numbers, $$T_1,T_2,...,T_{10}$$. For the remaining planes, the triangular shapes have truncated corners and the number of vertices on these planes are two sets of the numbers $$T_{11}-3T_1=63,T_{12}-3T_2=69,T_{13}-3T_3=73,T_{14}-3T_4=75.$$

Going across the cube, we then obtain the sum of squares by calculating $$2(\sum _{i=1} ^{10} T_i^2+63^2+69^2+73^2+75^2)=2(7942+19684)=55252.$$