Number of ways to write n as a sum of k nonnegative integers How many ways can I write a positive integer $n$ as a sum of $k$ nonnegative integers up to commutativity?
For example, I can write $4$ as $0+0+4$, $0+1+3$, $0+2+2$, and $1+1+2$.

I know how to find the number of noncommutative ways to form the sum: Imagine a line of $n+k-1$ positions, where each position can contain either a cat or a divider. If you have $n$ (nameless) cats and $k-1$ dividers, you can split the cats in to $k$ groups by choosing positions for the dividers: $\binom{n+k-1}{k-1}$. The size of each group of cats corresponds to one of the nonnegative integers in the sum.
 A: I have been recently solving a similar problem: how many ways can 1024 items fall into 16 histogram bins? This is essentially the same question, and the problem it coincides with is the number of unordered partitions (we don't care in which order the numbers are filled, writing e.g. 1024 = 512 + 256 + 256 + 0's is the same as 1024 = 0's + 256 + 512 + 256, or any other).
Since my problem is quite big, I've written C++ code to either just count the partitions, or also enumerate them. Get it at http://www.stud.fit.vutbr.cz/~xpolok00/proj/UnPart.h. One can use:
CUnorderedPartition(n, k).n_Partition_Num()

to get just the number of partitions. For n = 1024 and k = 16, it will return 6561153029810 in about two days of time on an average computer.
To also evaluate paritions, one can for example use:
CUnorderedPartition part(10, 3); // write 10 as a sum of 3 numbers
do {
    cout << part.r_Bin_Values()[0] << ", " <<
            part.r_Bin_Values()[1] << ", " <<
            part.r_Bin_Values()[2] << endl;
} while(part.b_Next()); // is there a next combination?
cout << "that was " << part.n_Partition_Num() << " partitions" << endl;

And that will write:
0, 0, 10
0, 1, 9
0, 2, 8
0, 3, 7
0, 4, 6
0, 5, 5
1, 1, 8
1, 2, 7
1, 3, 6
1, 4, 5
2, 2, 6
2, 3, 5
2, 4, 4
3, 3, 4
that was 14 partitions

You can see in the table that the result is correct. The values will always come out sorted in ascending order (since it generates unordered partitions). Enumerating the partitions is a bit slower, for 1024, 16 it takes about three days.
A: I have a much simpler approach using DP which does not take an exponential time to calculate the number of partition. I verified this with certain small values though for n=1024 and k=16 I got different answer. 
Following is the java implementation of the problem.
It has three independent part:


*

*Divide S in K parts. The parts can have value 0 and order matters. i.e. 10 = 0+5+5+0 and 10=5+5+0+0 are different

*Divide S in K parts. The parts can not have value 0 and order matters. i.e. 10 can not have value 0+5+5+0  but 10 can be 1+4+4+1 and 4+4+1+1

*Divide S in K parts. The parts can have value 0 but order does not matters. i.e. 10 = 0+5+5+0 and 10=5+5+0+0 are same.
public class Main {
public static void main(String[] args) throws Exception {
    int S = 10, K = 4;
    System.out.println("S=" + S + "   K=" + K);
    System.out.println(PartionwithZero(S, K));
    System.out.println(PartionwithoutZero(S, K));
    System.out.println(PartionwithZeroUnique(S, K));
}

/* Divide S in K parts 0 may be present */
public static long PartionwithZero(int S, int K) {
    long DP_Table[][] = new long[K][];
    for (int i = 0; i < K; i++)
        DP_Table[i] = new long[S + 1];
    for (int i = 0; i < S + 1; i++)
        DP_Table[0][i] = 1;
    for (int i = 0; i < K; i++)
        DP_Table[i][0] = 1;

    for (int i = 1; i < K; i++) {
        for (int j = 1; j < S + 1; j++)
            DP_Table[i][j] = DP_Table[i - 1][j] + DP_Table[i][j - 1];
    }

    /*
     * for(long i=0;i<K;i++) { for(long j=0;j<S+1;j++)
     * System.out.prlong(DP_Table[i][j]+" "); System.out.println(); }
     */
    return DP_Table[K - 1][S];
}

/* Divide S in K parts 0 should not be present */
public static long PartionwithoutZero(int S, int K) {
    long DP_Table[][] = new long[K][S];
    for (int i = 0; i < S; i++)
        DP_Table[0][i] = 1;
    for (int i = 1; i < K; i++)
        DP_Table[i][0] = 0;

    for (int i = 1; i < K; i++) {
        for (int j = 1; j < S; j++)
            DP_Table[i][j] = DP_Table[i - 1][j - 1] + DP_Table[i][j - 1];
    }

    /*
     * for(long i=0;i<K;i++) { for(long j=0;j<S;j++)
     * System.out.print(DP_Table[i][j]+" "); System.out.println(); }
     */
    return DP_Table[K - 1][S - 1];
}

/* Divide S in K parts 0 may be present */
public static long PartionwithZeroUnique(int S, int K) {
    long DP_Table[][][] = new long[K][S + 1][S + 1]; // DP_Table[no of
                                                        // partition][Sum][Maximum
                                                        // value in the
                                                        // partition]

    for (int i = 0; i < K; i++)
        DP_Table[i][0][0] = 1;
    for (int i = 1; i < S + 1; i++) {
        DP_Table[0][i][0] = 0;
        DP_Table[0][i][i] = 1;
    }
    for (int i = 1; i < S + 1; i++)
        DP_Table[0][0][i] = 0;

    for (int i = 0; i < S + 1; i++)
        for (int j = 0; j < i; j++)
            DP_Table[0][i][j] = 1;

    for (int i = 1; i < K; i++) {
        for (int j = 1; j < S + 1; j++)
            for (int k = 1; k < S + 1; k++) {
                // System.out.println(i+" "+j+" "+k);
                DP_Table[i][j][k] = (k - j) >= 0 ? DP_Table[i - 1][j][k - j]
                        : 0;
            }
        for (int k = 0; k < S + 1; k++) {
            long sum = 0;
            for (int j = 0; j < S + 1; j++) {
                DP_Table[i][j][k] += sum;
                sum = DP_Table[i][j][k];
            }
        }
    }

    /*
     * for(int i=0;i<K;i++) { System.out.println("K ="+i); for(int
     * j=0;j<S+1;j++) { for(int k=0;k<S+1;k++)
     * System.out.print(DP_Table[i][j][k]+" "); System.out.println(); }
     * System.out.println(); System.out.println(); }
     */
    return DP_Table[K - 1][S][S];
}

}
Following are the output of some cases:
S=6   K=3
28
10
7
S=10   K=3
66
36
14
S=20   K=12
84672315
75582
582
A: If you are only interested in a small:ish number of $k$ then this is most easily solved by thinking recursively and using induction.
First some notation. Let $F_k(n)$ be the number of ways to sum $k$ natural numbers so the sum is $n$.
The generic reasoning can be inferred from a small example.
Assume we have three numbers we want to sum to 4. The number of ways to do this is the same as setting the first digit to $k=4,3,2,1,0$ in turn and then using the remaining digits to sum up to $k-1$. 
number of ways to write 4 with three digits =
{4 + {number of ways to write 0 with two digits}} +
{3 + {number of ways to write 1 with two digits}} +
{2 + {number of ways to write 2 with two digits}} +
{1 + {number of ways to write 3 with two digits}} +
{0 + {number of ways to write 4 with two digits}}
(You might want to convince yourself that this is the case and there is no need to assume the set digit in different positions)
Which is the same as writing (in our notation)
$F_3(4) = F_2(0) + F_2(1) + F_2(2) + F_2(3) + F_2(4)$
For the general case we have
$F_k(n) = \sum_{l=0}^n F_{k-1}(l)$
it is also easily seen that $F_1(n)=1$ and $F_k(0)=1$. This now allows us to expand the first few relations as
$$F_1(n) = 1$$
$$F_2(n) = \sum_{l=0}^n F_1(l) = \frac{(n+1)}{1!}$$
$$F_3(n) = \sum_{l=0}^n F_2(l) = \sum_{l=0}^n n+1 = \frac{(n+1)^2+(n+1)}{2!}$$
$$F_4(n) = \sum_{l=0}^n F_3(l) = \frac{(n+1)^3+3(n+1)^2+2(n+1)}{3!}$$
$$F_5(n) = \sum_{l=0}^n F_4(l) = \frac{(n+1)^4 + 6(n+1)^3+11(n+1)^2+6(n+1)}{4!}$$
... and so on
Unfortunately there isn't any "nice" generic expression for the coefficients in the numerator. Its a good exercise to find it but be warned; it gets quite messy apart from the two highest and the lowest coefficients in the numerator polynomial which you probably can spot by inspection.
Addendum:
By factoring the numerator and recognizing the Gamma function the expression can be written in semi-closed form as:
$$F_k(n) = \frac {\Gamma \left( n+k \right) }{\Gamma \left( n+1 \right)  \left( 
k-1 \right) !}$$
A: A much simpler and mathematical approach:
This problem is equivalent to finding the co-efficient of $x^N$ in the expression  $f(x) = (1+x+x^2+x^3+\dots +x^N)^M$
Then  $f(x) = ((x^{(N-1)} - 1)/(x-1))^M$. Differentiate it $M$  times (so use $\frac{d^Nf(x)}{dx^N}$) and the co-efficient will be  $(\frac{1}{n!})\left(\frac{d^Nf(x)}{dx^N}\right)$ at $x = 0$; differentiation can be done using any numerical differentiation technique. So the complexity of the algorithm is $O(N\times \text{the complexity of differentiation})$. 
A: In python you can list the solutions recursively very simply:
k = 6
def a(n, m, l):
    if len(l) < k-1:
        for i in range(0, n+1):
            a(n-i, m-1, l+[i])
    else:
        print(l+[n])
        
a(10, k, [])

The above example prints all the solutions for n = 10 and k = 6.
A: Note : I Can't Use MathJax
First, think of the number n as the sum of n ones.
Now think of the k non-negative integers
So now consider this row of n+k-1 objects where there are the n ones and k-1 walls.
We Arrange these (k-1) walls in this row .
Now the k-1 walls cordon off k numbers who shall add up to the number n.
Note that the walls can be adjacent as they shall cordon off zero and zero is non-negative.
So to summarize the total number of ways to do this is the number of arrangements of this row. This is obviously (n+k-1) choose (k-1) as the selection as the selection of the (k-1) places where the walls must go completely determines the rest of the row .
Hope this helps!
P.S ( This Is My First answer on math s.e)
