What combination of numbers makes a specific sum?
The code below is not very effective, because it also gives answers in which a number is used more than once even though it was given in the list only once.
IntegerPartitions[45, {4}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]
{{10, 10, 10, 5}, {10, 10, 9, 6}, {10, 10, 8, 7}, {10, 9, 9, 7}, {10, 9, 8, 8}, {9, 9, 9, 8}}
Notice that $10$ is given just once as a number in the last argument of IntegerPartitions
, yet it appears three times in the first answer, and twice in the second; likewise with $9$.
So how can Mathematica be coded to return answers that have unique elements?
P.S I know FrobeniusSolve
does not work as it only returns how many times numbers/(elements of answers) appear in a specific answer.
Select
, egSelect[ IntegerPartitions[35, {4}, Range[0, 12]] , Length@Union@# == 4 & ]
$\endgroup$Apply@Greater
instead ofLength@Union@# == 4 &
for this filtering approach, but this works just because these results have a descending order. $\endgroup$DuplicateFreeQ
. $\endgroup$Select[Subsets[yourList,{subsetLength}],Total@#==sum&]
e.g.Select[Subsets[Range[10],{4}],Total@#==25&]
$\endgroup$IntegerPartitions[45, {4}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}]
gives{}
. It pays to post correctly functioning code, else it is just confusing. $\endgroup$