2
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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)$). $\endgroup$
    – anderstood
    Mar 16, 2017 at 0:23
  • $\begingroup$ "At least"?? So you're interested in collections that might give a higher number too? That unbounds your search. $\endgroup$ Mar 16, 2017 at 0:54
  • $\begingroup$ @DavidG.Stork Yes; at least indeed... but the size of the list is not unlimited... $\endgroup$ Mar 16, 2017 at 0:55
  • $\begingroup$ So you DO want to allow higher numbers! OK. $\endgroup$ Mar 16, 2017 at 0:56
  • $\begingroup$ Possible duplicate mathematica.stackexchange.com/questions/22397/… $\endgroup$ Mar 16, 2017 at 1:09

2 Answers 2

5
$\begingroup$

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 &]
$\endgroup$
6
  • $\begingroup$ The question states "at least", so you should use $>$ (not $<$). But I think the poser is mistaken in what he wants. $\endgroup$ Mar 16, 2017 at 0:55
  • $\begingroup$ you are right! fixed $\endgroup$
    – ZaMoC
    Mar 16, 2017 at 0:57
  • $\begingroup$ 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? $\endgroup$ Mar 16, 2017 at 1:11
  • $\begingroup$ ListPlot[DeleteDuplicates[ Select[Subsets[{13, 15, 19, 19, 23, 31}], Total[#] >= 80 &]]] $\endgroup$
    – ZaMoC
    Mar 16, 2017 at 1:16
  • $\begingroup$ @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. $\endgroup$ Mar 16, 2017 at 1:43
0
$\begingroup$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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