# How to find all ways to obtain an integer n as the sum of m integers (without order)?

I'm looking for an algorithm that finds all ways to express an integer n as the sum of m (non-negative) integers. I am in particular interested in $m=6$ and $n\leqslant20$. What would be the fastest way to find all possibilities (using a computer, not by hand). If possible, I would like to only look at combinations of six integers, with order not being relevant (that is, [1, 2, 0, 0, 0, 0] and [2, 1, 0, 0, 0, 0] are counted as 1 combination).

The simplest way would be simply trying all permutations with 6 integers lesser than or equal to 20 and only adding the ones that sum up to 20 to our result (followed by removing doubles if we do not want to look at the ordering). This seems like it would take awfully long however, since $20^6$ possibilities will take quite a while to check.

What would be a more efficient way to tackle this?

You are looking for the partitions of $20$ into $6$ parts. This is the same as the partitions of $20$ with largest part $6$ and any number of parts, which is the same as the partitions of $14$ with largest part at most $6$. The Wikipedia article gives a recurrence.
• I would list them by largest part. The largest part has to be at least $4$. If the largest part is $8$, say, the rest is a partition of $12$ with largest part at most $8$. This suggests a recursive algorithm with inputs of the number you are making partitions of, the max allowable part, and the number of parts. Each one calls the same routine with what is left. – Ross Millikan Jan 24 '17 at 20:22