Combinatorics – Counting Ways to Write a Number as Sum of Distinct Numbers

combinatoricsinteger-partitions

I am interested in counting the number of ways in which a number $n$ can be written as the sum of $k$ distinct numbers in increasing order such that every number is a natural number in the range $\{1,2,\dots,\ell\}$.

For example, with $n=23$ and $\ell=9$ we have for the following values of $k$:

$n=23,\ell=9,k=7$: There are zero ways as the smallest summation possible is $28$.

$n=23,\ell=9,k=6$: There are two ways, namely $1+2+3+4+5+8$ and $1+2+3+4+6+7$

$n=23,\ell=9,k=5$: There are several ways, but I have difficulty counting them by hand

$n=23,\ell=9,k=4$: There are several ways, but I have difficulty counting them by hand

$n=23,\ell=9,k=3$: There is only one way, namely $6+8+9$

$n=23,\ell=9,k=2$: There are zero ways as the largest possible summation totals $17$


Given certain values of $n,k,\ell$, what type of techniques can be employed to count the number of summations? What algorithms might we employ if we were to try to brute force the count with a computer? What is a more common name for the objects I am trying to count and do these appear anywhere in literature?

Related: In how many ways can I write a number $n$ as the sum of $4$ numbers?

Best Answer

You can define this as a recurrence relation:

$$f(n, \ell, k) = \begin{cases} 1, & \text{if $n = k = 0$} \\ 0, & \text{if $n < 0$ or $k < 0$} \\ \sum_{i = 1}^{\ell}{f(n-i, i-1, k-1)}, & \text{otherwise} \end{cases}$$

This can be optimized when computing $f(n, \ell, k)$ by using memoization.

As @JMoravitz mentioned:

The specific objects you are trying to count are restricted partitions of n into k distinct nonempty parts, each of size no greater than l.

Related Question