# How to find the solutions to $a+b+c+d=60$?

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.

• The variables are supposed to have integer values? Jun 13, 2015 at 17:51
• @J. M. No, non-negative integers. Jun 13, 2015 at 18:01

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]
]

• OP's equation has infinitely many solutions, unless one enforces the constraint you are implying in this answer. Jun 13, 2015 at 18:00
• @Guesswhoitis. Yes. I made a crappy question (forgot the constraint) but somehow, he read my mind and guessed correctly what I was doing. Jun 13, 2015 at 18:02

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"}]


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]