9
$\begingroup$

How can I find all the solutions to:

$$a+b+c+d=60\quad (0\leq a,b,c,d\leq 30,\; a,b,c,d\in\mathbb N)$$

I've tried to use Solve[], but it says that there are more variables than equations. I want to know if Mathematica has a built-in way to do it, I know how to find the number of solutions (which is a basic result in combinatorics), I also know that I could make a messy thing to find them.

$\endgroup$
  • 2
    $\begingroup$ The variables are supposed to have integer values? $\endgroup$ – J. M. will be back soon Jun 13 '15 at 17:51
  • $\begingroup$ @Guesswhoitis. No, non-negative integers. $\endgroup$ – Billy Rubina Jun 13 '15 at 18:01
14
$\begingroup$

FrobeniusSolve is useful for these kinds of equations. Your constraints may be implemented by using Pick as follows.

Block[{s = FrobeniusSolve[{1, 1, 1, 1}, 60]},
   Pick[s, UnitStep[30 - s[[All, 1]], 30 - s[[All, 2]], 
                    30 - s[[All, 3]], 30 - s[[All, 4]]], 1]
]
$\endgroup$
  • $\begingroup$ OP's equation has infinitely many solutions, unless one enforces the constraint you are implying in this answer. $\endgroup$ – J. M. will be back soon Jun 13 '15 at 18:00
  • $\begingroup$ @Guesswhoitis. Yes. I made a crappy question (forgot the constraint) but somehow, he read my mind and guessed correctly what I was doing. $\endgroup$ – Billy Rubina Jun 13 '15 at 18:02
10
$\begingroup$

For "all" solutions use Reduce. Assuming that the intended domain is Integers,

Reduce[{a + b + c + d == 60, a <= 30, b <= 30, c <= 30, d <= 30}, {a, b, c, 
  d}, Integers]

(a | b | c | d) [Element] Integers && -30 <= a <= 30 && ((b == -a && c == 30 && d == 30) || (-a < b <= 30 && 30 - a - b <= c <= 30 && d == 60 - a - b - c))

For nonnegative integers,

Reduce[{a + b + c + d == 60, 0 <= a <= 30, 0 <= b <= 30, 0 <= c <= 30, 
  0 <= d <= 30}, {a, b, c, d}, Integers]

(a | b | c | d) [Element] Integers && ((a == 0 && ((b == 0 && c == 30 && d == 30) || (1 <= b <= 29 && 30 - b <= c <= 30 && d == 60 - b - c) || (b == 30 && 0 <= c <= 30 && d == 30 - c))) || (1 <= a <= 29 && ((0 <= b < 30 - a && 30 - a - b <= c <= 30 && d == 60 - a - b - c) || (b == 30 - a && 0 <= c <= 30 && d == 30 - c) || (30 - a < b <= 30 && 0 <= c <= 60 - a - b && d == 60 - a - b - c))) || (a == 30 && ((b == 0 && 0 <= c <= 30 && d == 30 - c) || (1 <= b <= 29 && 0 <= c <= 30 - b && d == 30 - b - c) || (b == 30 && c == 0 && d == 0))))

For specific examples, use FindInstance

Manipulate[
 FindInstance[{a + b + c + d == 60, 0 <= a <= 30, 0 <= b <= 30, 0 <= c <= 30, 
   0 <= d <= 30}, {a, b, c, d}, Integers, n],
 {{n, 10, "Instances"}, 1, 100, 1, Appearance -> "Labeled"}]

enter image description here

$\endgroup$
10
$\begingroup$

This returns some of them:

cs = PadRight[Select[IntegerPartitions[60, 4], And @@ Thread[# <= 30] &], {Automatic, 4}]

This returns all of them:

Flatten[Permutations /@  cs, 1]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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