# How many combinations of certain variables add up to at least n?

I have a set of numbers, let's say: a=16 b=19 c=19 d=31 e=5 f=... etc.

How to create a simple function that shows how many combinations of the letters add up to at least the number n?

I have no clue what Mathematica Functions I should use to get started. Any help would be appreciated.

• As a starting point, you could use Tuple to generate all combinations of letters of a given length; however some combinaison would be counted twice (e.g. $(a,b)$ and $(b,a)$). Mar 16, 2017 at 0:23
• "At least"?? So you're interested in collections that might give a higher number too? That unbounds your search. Mar 16, 2017 at 0:54
• @DavidG.Stork Yes; at least indeed... but the size of the list is not unlimited... Mar 16, 2017 at 0:55
• So you DO want to allow higher numbers! OK. Mar 16, 2017 at 0:56
• Possible duplicate mathematica.stackexchange.com/questions/22397/… Mar 16, 2017 at 1:09

n=80

number of subsets

Length@Select[Total /@ Subsets[{13, 15, 19, 19, 23, 31}], # >= 80 &]


and if you want to see the selected subsets type

Select[Subsets[{13, 15, 19, 19, 23, 31}], Total[#] >= 80 &]

• The question states "at least", so you should use $>$ (not $<$). But I think the poser is mistaken in what he wants. Mar 16, 2017 at 0:55
• you are right! fixed Mar 16, 2017 at 0:57
• Great! This is almost what I need! Now, how do I visualize the lists that actually fall within the right category? And doesn't this solution also allow for duplicates? Mar 16, 2017 at 1:11
• ListPlot[DeleteDuplicates[ Select[Subsets[{13, 15, 19, 19, 23, 31}], Total[#] >= 80 &]]] Mar 16, 2017 at 1:16
• @Jenny_mathy This is the endresult: Length@Select[Subsets[{31, 9, 19, 14, 19, 19, 6, 16, 3, 5, 4, 3, 2}], Total[#] > 75&] It's the amount of possible coalitions after the Dutch elections tonight. Mar 16, 2017 at 1:43

First create a list of your allowable component integers:

myset = {1, 2, 3, 5, 20};


Then:

Length[Select[IntegerPartitions[20], ContainsOnly[myset]]]


(* 92 *)

IntegerPartitions gives a list of all the says you can add integers to give the candidate number. Select from all those possible lists the ones that contain only the elements in your allowable component integers using ContainsOnly (version 10). Then count (Length) that list.

If you must, you can kludge a function using set operations to select lists whose union with myset is myset.

• ContainsOnly? I don't have that function...? Mar 16, 2017 at 0:50
• @David G. Stork You can directly use IntegerPartitions[20, All, {1, 2, 3, 5, 20}] // Length, instead of using Select. Mar 16, 2017 at 1:04
• @AnjanKumar: Ah yes... I didn't know that IntegerPartitions had that option. Thanks. Mar 16, 2017 at 1:12