Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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 '14 at 12:31

Your Answer


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.