0
$\begingroup$

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}, ....

$\endgroup$
6
$\begingroup$
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}]
| improve this answer | |
$\endgroup$

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.