1
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thank you, now I know the name of what I'm looking for. Do you also happen to know how to find all partitions themselves, rather than just the amount that exist? The formula does not seem to help me with that. $\endgroup$ – Dasherman Jan 24 '17 at 19:44
  • $\begingroup$ 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. $\endgroup$ – Ross Millikan Jan 24 '17 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.