This is essentially ordering the lattice points intersected by a plane in three space in the all-positive-or-zero octant where the sum of the $x,y,z$-coordinates is $n$. (I may be mistaken but your example for n=1,2,3 show the values for n=0,1,2, and your example for n=4 does show the answer for 4, so I think you're off by one for part of your example. And you use the word *permutation*, but your description of the problem is more aptly states as *combination* of three integers.) This is essentially a simple geometric problem in 3-space over the integers, unless I am misunderstanding your question. If you look at it geometrically, you are looking at the points on the lattice $\mathbb {Z}^3$ and finding the points on the plane $x+y+z=n$ in the all positive-or-zero-octant $x\ge0, y\ge0, z\ge0$. You can then order the lattice points which satisfy these constraints ordinally in whatever order you prefer, say numerically with $x$-coordinate taking precedence over $y$ taking precedence over $z$. In this case, it is easy to read off the coordinates of the triangle in the all-positive octant of 3-space. for x = 0 to n for y = 0 to n-x z = n-x-y print x,y,z next y next x