1

The number of $k$-compositions of a positive integer $n$ is ${n-1}\choose{k-1}$, and its number of $k$-weak compositions is ${n+k-1}\choose{k-1}$.

However, how to calculate the corresponding compositions when we restrict that the numbers of the sequence cannot be greater than a certain value?

For example, suppose we have the list of all possible tickets with numbers of $6$ digits, where each digit can be a value between $0$ and $9$, and we want to calculate in how many of them their sum is $20$. (Since they are ticket numbers, as in a lottery, they can start with zeros on the left). The result should be the $6$-weak compositions of $20$ with the restriction that the values cannot be greater than $9$.

1 Answers1

1

One way is through recursion. Let $N(k,m)$ be the number of $k$-weak compositions of $m$ into numbers in $\{0,\ldots,9\}$. I don't know the answer immediately, but I can deduce $$ N(k,m)=\sum_{i=0}^9 N(k-1,m-i) $$ (by choosing the first number as one of $0,\ldots,9$) and the boundary conditions $N(1,i)=1$ when $i \in \{0,\ldots,9\}$ and $N(1,i)=0$ otherwise.

And we can implement this e.g. in GAP via:

NumComps:=function(k,m)
  if(m<0) then
    return 0;
  fi;

if(k=1) then if m in [0..9] then return 1; fi; return 0; fi;

return Sum([0..9],i->NumComps(k-1,m-i)); end;;

And if we run NumComps(6,20); we get 35127.

Oh, and we can check the number via brute force. The compositions are given in GAP by Filtered(Tuples([0..9],6),R->Sum(R)=20); which has size 35127.