Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

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}]
share|improve this answer
    
thanks a lot!!! –  Winnie Lee Feb 27 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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