An algorithm to generate all unique combinations of addends for a sum, from a range of small addends which are greater than 1?

286 Views Asked by At

I'm looking for an algorithm to generate all unique combinations of addends for a given sum, within a certain given range of addends. The size of the sum could range from two digits to five digits, while the minimum and maximum of the range of addends will generally be positive single-digit numbers greater than $1$.

I would also be happy to find an algorithm that generates all permutations of such addends within the range because I think I could figure out how to eliminate the duplicates.

The application of this problem is dividing a group of legislators into a number of multi-member districts of limited size. If we have $L = 19$, minimum district size $min = 3$, and maximum district size $max = 6$, the unique combinations I generated by hand are:

  • $(4, 3, 3, 3, 3, 3)$
  • $(4, 4, 4, 4, 3)$
  • $(5, 5, 5, 4)$
  • $(5, 4, 4, 3, 3)$
  • $(6, 5, 5, 3)$
  • $(6, 4, 3, 3, 3)$
  • $(6, 5, 4, 4)$
  • $(6, 6, 4, 3)$

I found this question which seems relevant but I have no math background, so I didn't really understand what I read there or when I followed the link to Wikipedia's page on partitions.