# Counting number of ways a number can be written as the sum of distinct numbers within a certain range

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?

• Welcome to MSE! Before posting questions you should read our guide on how to use MathJax so to write prettier equations and you should even read our guidline to write better questions so that users will be more inclined to answer! – Davide Morgante Jul 20 '18 at 22:01
• I have taken the liberty to heavily rewrite your question, hopefully making it more clear what is being asked. In doing so, I relabeled some of your variables. Rewriting an earlier comment, the specific objects you are trying to count are restricted partitions of $n$ into $k$ distinct nonempty parts, each of size no greater than $\ell$. – JMoravitz Jul 20 '18 at 23:09

$$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.