Find the sets of sums
Pyth, 14 bytes
V^SQEIqsNQj\,N
Try it online: Demonstration or Test Suite
Explanation:
V^SQEIqsNQj\,N implicit: Q = first input number
SQ create the list [1, 2, ..., Q]
E read another number
^ cartesian product of the list
this creates all tuples of length E using the numbers in SQ
V for each N in ^:
IqsNQ if sum(N) == Q:
j\,N join N by "," and print
Python 3, 77 bytes
def f(n,m,s=''):[f(i,m-1,',%d'%(n-i)+s)for i in range(n)];m|n or print(s[1:])
A recursive function that builds each output string and prints it. Tries every possible first number, recursing down to find a solution with the corresponding decreased sum n
, and one fewer summand m
, and a string prefix s
with that number. If both the required sum and the number of terms are equal 0, we've hit the mark, so we print the result, cutting off the initial comma. This is checked as m|n
being 0 (Falsey).
79 chars in Python 2:
def f(n,m,s=''):
if m|n==0:print s[1:]
for i in range(n):f(i,m-1,','+`n-i`+s)
CJam, 22 bytes
q~:I,:)m*{:+I=},',f*N*
Try it online in the CJam interpreter.
How it works
q~ Read and evaluate all input. Pushes n and m.
:I Save m in I.
,:) Turn it into [1 ... I].
m* Push all vectors of {1 ... I}^n.
{ }, Filter; for each vector:
:+I= Check if the sum of its elements equals I.
Keep the vector if it does.
',f* Join all vectors, separating by commas.
N* Join the array of vectors, separating by linefeeds.