Skip to main content

syntax - How to set similar constraints on multiple variables in Solve


In this equation:


var = {x, y, z, m};
Solve[x + 2y + z + m == 14 && And @@ Thread[2 <= var <= 3], var, Integers]


{{x->2,y->3,z->3,m->3},{x->3,y->3,z->2,m->3},{x->3,y->3,z->3,m->2}}



But I think ...And @@ Thread[2 <= var <= 3]...Integers is very ugly. So I give this another try as follows:


var = {x, y, z, m};

Solve[x + 2y + z + m == 14 && (Alternatives@@ var == (2|3)), var]

But I get some error information.


I would like to know whether there are better methods to do do this.



Answer



In version 10, you can use this simpler syntax without Thread:


Solve[x+2y+z+m==14&&2<=var<=3,var,Integers]


{{x->2,y->3,z->3,m->3},{x->3,y->3,z->2,m->3},{x->3,y->3,z->3,m->2}}




Comments