Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
    
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 – andre Jan 17 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". – Eric Towers Jan 17 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  *)
share|improve this answer
    
Thanks, is there a way to export all of those non-negative solutions to some *.csv file for example? – Tomasz Kowalczyk Jan 17 at 18:59
3  
@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 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. – Bob Hanlon Jan 17 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.

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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