1

I want to find all subsets which the total of elements is the same.

For example, Subsets[Range[30], {5}] means all 5-element subsets and the range of each element is from 0 to 30. But I want to get the subsets which are only the total of elements equals to 30 (e.g., {6, 6, 6 ,6, 6}, {7, 5, 6, 6, 6}, ....

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Winnie Lee
  • 41
  • 1

1 Answers1

6
Select[Subsets[Range[30], {5}], Total[#] == 30 &]

Will give you the desired subsets.

Note that for this particular kind of problem

Sort[Sort /@ Select[IntegerPartitions[30, {5}], Unequal @@ # &]]

will give you the same results, much more quickly.

Note that your question is ambiguous: {6,6,6,6,6} is not a "subset" by definition.

If you meant partitions, the following will do it:

IntegerPartitions[30, {5}]
ciao
  • 25,774
  • 2
  • 58
  • 139