2
$\begingroup$

This question (21008) asks to find all permutations of {a, b, c} subject to a + b + c = n. The answer was provided by Dr. belisarius using IntegerPartitions.

How can we generalize that answer to find all permutations of {a, b, c} subject to 2 a + b + c = const, or more generally find all partitions subject to {a, b, c, d, e, ...} subject to 2 a + b + c + d + e + ... = n?

$\endgroup$
3
$\begingroup$

Are you looking for FrobeniusSolve? e.g. for $2 a + b + c = 5$

FrobeniusSolve[{2, 1, 1}, 5]
{{0, 0, 5}, {0, 1, 4}, {0, 2, 3}, {0, 3, 2}, {0, 4, 1}, {0, 5, 0},
 {1, 0, 3},  {1, 1, 2}, {1, 2, 1}, {1, 3, 0}, {2, 0, 1}, {2, 1, 0}}
$\endgroup$
2
  • $\begingroup$ * facepalm! * If the answer is that simple, then the best thing to do is to accept this answer, and close this question because "it can easily be found in the documentation". $\endgroup$
    – QuantumDot
    Jun 11 '17 at 20:16
  • $\begingroup$ @QuantumDot It happens to all of us. :^) $\endgroup$
    – Mr.Wizard
    Jun 11 '17 at 20:20
1
$\begingroup$

How about:

g[sz_, n_] := Partition[Flatten[Map[Thread[{
    #/2, f[n - #, sz - 1]}] &, Range[0, n, 2]]], sz]
g[3, 5]

{{0, 5, 0}, {0, 0, 5}, {0, 4, 1}, {0, 1, 4}, {0, 3, 2}, {0, 2, 3}, {1, 3, 0}, {1, 0, 3}, {1, 2, 1}, {1, 1, 2}, {2, 1, 0}, {2, 0, 1}}

where f is defined in the question you mentioned:

f[sum_, quant_] := Flatten[Permutations /@
                    IntegerPartitions[sum, {quant}, Range[0, sum]], 1]
$\endgroup$

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