# Number of solutions for inequality

Please explain me how can I calculate a number of possible solutions for such inequality and obtain those values:

3x + 7y + z <= 198


where x,y,z are integers.

• reference.wolfram.com/language/tutorial/… Jan 17, 2016 at 17:58
• brutal force : Table[If[3 x + 7 y + z <= 198, {x, y, z}, 0], {x, 198}, {y, 198}, {z, 198}] // Flatten[#, 2] & // DeleteCases[#, 0] & //Length Jan 17, 2016 at 18:21
• Since there's no lower bound on $x,y,z$, there are infinitely many answers. For example, (- a lot, - a lot, - a lot). You probably mean to say "... where $x,y,z$ are positive integers." You may also mean "non-negative". Jan 17, 2016 at 22:17

There are an infinite number of solutions unless you constrain x, y, z more than just to being integers. For example, let {x, y, z} all be negative.

Using the asumption that they are all non-negative

Solve[{3 x + 7 y + z <= 198, x >= 0, y >= 0, z >= 0}, {x, y, z},
Integers] // Length

(*  67354  *)


Using the asumption that they are all positive

Solve[{3 x + 7 y + z <= 198, x > 0, y > 0, z > 0}, {x, y, z},
Integers] // Length

(*  57033  *)

• Thanks, is there a way to export all of those non-negative solutions to some *.csv file for example? Jan 17, 2016 at 18:59
• @Tomasz Kowalczyk Export["solutions.csv", {x,y,z}/.Solve[{3x+7y+z<=198, x>=0, y>=0, z>=0}, {x, y, z}, Integers]]
– Bill
Jan 17, 2016 at 19:08
• Or Export["solutions.csv", {x,y,z,3x+7y+z}/.Solve[{3x+7y+z<=198, x>=0, y>=0, z>=0}, {x, y, z}, Integers]] to see the function value as well. Jan 17, 2016 at 20:12

Since you seem to want non-negative solutions, try FrobeniusSolve, which is built for linear Diophantine problems such as yours. Count such solutions with

Sum[Length[FrobeniusSolve[{3, 7, 1}, b]], {b, 0, 198}]


and find the solutions with

Flatten[Table[FrobeniusSolve[{3, 7, 1}, b], {b, 0, 198}], 1]


On multi-core machines, use ParallelSum and ParallelTable for faster results. See this question for more information.

Another way:

FindInstance[
3 x + 7 y + z <= 198 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers, 5]

{{x -> 43, y -> 9, z -> 5}, {x -> 50, y -> 4, z -> 15}, {x -> 35, y -> 9, z -> 22},
{x -> 34, y -> 10, z -> 26}, {x -> 24, y -> 17, z -> 3}}


verify it

3 x + 7 y + z <= 198 /. %
{True, True, True, True, True}


If more or less solutions be sought, change the last digit (5)