# How many possibilities of writing a natural number $M$ as a sum of $N$ natural numbers between $0$ and $M$?

How many possibilities are there of writing a natural number $M$ as a sum of $N$ natural numbers between $0$ and $M$?

For example, I need to write $4$, using $4$ numbers between $0$ and $4$. The possibilities are $$\begin{eqnarray*} 0+0+1+3 = 4 \\ 0+0+0+4 = 4 \\ 1+1+1+1 = 4 . \end{eqnarray*}$$

• So you want to count the number of partitions of $8$ into $4$ (non-zero) parts, or in general of $2M$ into $M$ parts. – André Nicolas Jun 9 '15 at 19:13
• See en.wikipedia.org/wiki/…. There is no closed-form formula, but you can calculate the number of partitions using recurrence relations or generating functions. – user7530 Jun 9 '15 at 19:15
• The formula should be ${M+N \choose N}$, I believe. – abnry Jun 9 '15 at 19:16
• The range of responses in Comments reflects the uncertainty whether the order of summands is considered in counting. Your examples suggest the order doesn't matter, but it would be better to state this explicitly. In that case you are asking about partitions of $M$ with at most $N$ parts. – hardmath Jun 9 '15 at 19:23
• Please see the problem : [link] codechef.com/JUNE15/problems/STDYTAB – Habib Rahman Jun 9 '15 at 19:36

The answer is $$p\left(M,N\right)=\binom{M+N-1}{N-1}=\frac{\left( M+N-1\right)\;!}{M\;!\left( N-1\right)\;!} \tag{1}$$ but concerns ordered solutions $\left(x_{1},x_{2},\cdots ,x_{N}\right)$ : $$x_{1}+x_{2}+\cdots +x_{N}=M \tag{2}$$ So for $M=4$ and $N=4$ $$p\left(4,4\right)=\binom{4+4-1}{4-1}=\frac{7\;!}{4\;!3\;!}=\frac{7\cdot 6 \cdot 5}{3 \cdot 2}=35 \tag{3}$$

The solutions $\left(0,1,3,0\right)$ and $\left(1,0,3,0\right)$ are considered different although they have the same summands.