3
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ reference.wolfram.com/language/tutorial/… $\endgroup$ – Karsten 7. Jan 17 '16 at 17:58
  • $\begingroup$ 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 $\endgroup$ – andre314 Jan 17 '16 at 18:21
  • $\begingroup$ 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". $\endgroup$ – Eric Towers Jan 17 '16 at 22:17
8
$\begingroup$

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  *)
$\endgroup$
  • $\begingroup$ Thanks, is there a way to export all of those non-negative solutions to some *.csv file for example? $\endgroup$ – Tomasz Kowalczyk Jan 17 '16 at 18:59
  • 3
    $\begingroup$ @Tomasz Kowalczyk Export["solutions.csv", {x,y,z}/.Solve[{3x+7y+z<=198, x>=0, y>=0, z>=0}, {x, y, z}, Integers]] $\endgroup$ – Bill Jan 17 '16 at 19:08
  • $\begingroup$ 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. $\endgroup$ – Bob Hanlon Jan 17 '16 at 20:12
5
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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)

$\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.