I'm seeking to write an algorithm which, given a value of N, will fill a matrix consisting of (N+1)(N+2)(N+3)/6 rows and 4 columns with the integers from 0, ... , N, subject to the conditions that:
- The sum of values in any row is N
- No rows are repeated (this should ensure that every such possibility is listed)
For example, with N=2, we have 10 (=3*4*5/6) rows:
2 0 0 0
1 1 0 0
0 2 0 0
1 0 1 0
1 0 0 1
0 1 1 0
0 1 0 1
0 0 2 0
0 0 1 1
0 0 0 2
I've been trying for a while to program this (in R, for what it's worth), but I'm struggling to get anywhere. Any advice?