2

I need to find the no of ways of partitioning a number N as a sum of K non-negative numbers.

Zeroes are also needed to be included in the sum.

Ordering does matter.

Example-

For $N=2,K=3 $

There are $6$ ways {1,1,0},{1,0,1},{0,1,1},{2,0,0},{0,0,2},{0,2,0}

I need an efficient recursive relation for this ?

LTim
  • 75
  • If ordering matters then this is called compositions, not partitions, and the formula is pretty straightforward (if memory serves correctly). – barak manos Jun 10 '15 at 15:35

2 Answers2

2

This is the bars and stars problem. Your problem is equivalent to this one:

How many ways are there to rearrange $N$ stars and $K-1$ bars? Answer: $\binom {N+K-1}{K-1}$

Think in the stars as balls that you have to put in a row of boxes, and think in the bars as the walls between boxes.

ajotatxe
  • 65,084
  • Is it matching with $N=5$ and $K=3$ ... Your formula shows $\dfrac{7!}{2! 5!}=21$...I couldn't get as many...(am i wrong?) – NeilRoy Jun 10 '15 at 15:32
  • 1
    With two numbers equal: $5+0+0$, $3+1+1$, $1+2+2$. Since there are three permutations for each, this makes $9$ ways. With pairwise different numbers: $0+1+4$, $0+2+3$. Since there are six permutations for each, this makes $12$ ways. $12+9=21$. – ajotatxe Jun 12 '15 at 16:28
  • Yeah that makes sense...my bad! – NeilRoy Jun 12 '15 at 16:59
0

Yes. Either the first number is zero, and you want $k-1$ numbers adding to $n$, or the first number is not zero. In the second case, find $k$ numbers that add to $n-1$, then add $1$ to the first number.

So $F(n,k)=F(n,k-1)+F(n-1,k)$
Use that to build up a bunch of values of $F(n,k)$ for, say $n=1..4$, $k=1..4$.

Empy2
  • 50,853