-1

I need find a recursive function ( Withdrawal formula ) $\operatorname{f}\left(n,k\right)$ for the problem:

  • What is the number of ways to write $n$ as a sum of $k$ different nonnegative integers, including zero ?.
  • examples for $n = 6,\ k = 3:\ 0 + 4 + 2,\ 1 + 5 + 0,\ 1 + 2 + 3$. $\mbox{So,}\ \operatorname{f}\left(6,3\right) = 3$. The answer should be without SIGMA ( summing ).

My attempt:

  • I thought to look at the first number, but it doesnt help, because i canot guarantee it wont appears in $\operatorname{f}\left(n - i,k - 1\right)$.
  • I thought maybe look at the problem as building a increasing monotonous series of $$ \left\{0,\ldots,n\right\}\quad\mbox{of length}\quad K $$
    • But again dont know how to move from here.
Felix Marin
  • 94,079

1 Answers1

1

A linked question has an answer with a simpler recurrence that can be modified for this question, and I would prefer that more than the following mess with extra function argument.


Previous revision

Let $g(n,k,m)$ be the number of ways to write $n$ as a sum of $k$ different integers within $[0,m]$, i.e. $m$ is the maximum allowable summand.

For the main recurrence case, a way to write $n$ as a sum of $k$ different integers within $[0,m]$ either:

  1. has $m$ as one summand, with $k-1$ other summands of integers within $[0,m-1]$ that add to $n-m$; or
  2. doesn't have $m$ as a summand, but $k$ summands of integers only within $[0,m-1]$ that add to $n$.

Then a recursive definition of $g$ could be:

$$\begin{align*} g(0,0,m) &= 1\\ g(n,k,m) &= 0, && n<0 \lor k<0 \lor m\le 0\\ g(n,k,m) &= g(n-m,k-1,m-1) + g(n,k,m-1),&& \text{otherwise} \end{align*}$$

And then $f$ may be defined as

$$f(n,k) = g(n,k,n)$$


For example $n=6$, $k=3$,

$$\begin{align*} f(6,3) &= g(6,3,6)\\ &= g(0,2,5) + g(6,3,5)\\ &\vdots\\ &= g(0,2,5) + g(1,2,4)+g(2,2,3) + g(3,2,2) + g(4,2,1) + g(5,2,0)\\ &= g(0,2,0) + {[g(0,1,0)+g(1,2,0)]} + {[g(0,1,1)+g(1,1,0)+g(2,2,0)]} + {[g(1,1,1)+g(2,1,0)+g(3,2,0)]} + {[g(3,1,0)+g(4,2,0)]} + 0\\ &= 0+{[g(0,0,-1)+0]} + {[g(0,1,0)+0+0]} + {[g(0,0,0)+0+0]} + {[0+0]}+0\\ &= 0+g(0,0,-1)+g(0,0,-1)+g(0,0,0) +0+0\\ &= 3 \end{align*}$$

peterwhy
  • 22,930