How do I generate a set of n-tuples containing integral solutions to a linear equation provided certain constraints?
Let m,k,p be fixed positive integers. I want to create a table of k-tuples (x1,x2,…,xk) comprised of solutions in positive integers to the equation below:
x1+x2+⋯+xk=mwhere 1≤x1,x2,…,xk≤p
For example, if I set m=16,k=3, and p=6. How can I make mathematica generate the set (table) of integer triples (x1,x2,x3) such that x1+x2+x3=16
Also, how would I seperately generate a table of positive integral triples (x1,x2,x3) for which x1+x2+x3≥16and 1≤x1,x2,x3≤6.
Answer
Try to read Documentation on functions Solve and Reduce and tutorial Solving Equations. Look through this forum, I bet this is a duplicate question. There is also a guide: Diophantine Equations.
{x1, x2, x3} /.
Solve[x1 + x2 + x3 == 16 && 1 <= x1 <= 6 && 1 <= x2 <= 6 &&
1 <= x3 <= 6, {x1, x2, x3}, Integers]
{{4, 6, 6}, {5, 5, 6}, {5, 6, 5}, {6, 4, 6}, {6, 5, 5}, {6, 6, 4}}
or alternatively
Reduce[x1 + x2 + x3 == 16 && 1 <= x1 <= 6 && 1 <= x2 <= 6 &&
1 <= x3 <= 6, {x1, x2, x3}, Integers]
(x1 == 4 && x2 == 6 && x3 == 6) || (x1 == 5 && x2 == 5 && x3 == 6) || (x1 == 5 && x2 == 6 && x3 == 5) || (x1 == 6 && x2 == 4 && x3 == 6) || (x1 == 6 && x2 == 5 && x3 == 5) || (x1 == 6 && x2 == 6 && x3 == 4)
Your 2nd question would be
{x1, x2, x3} /.
Solve[x1 + x2 + x3 >= 16 && 1 <= x1 <= 6 && 1 <= x2 <= 6 &&
1 <= x3 <= 6, {x1, x2, x3}, Integers]
{{4, 6, 6}, {5, 5, 6}, {5, 6, 5}, {5, 6, 6}, {6, 4, 6}, {6, 5, 5}, {6, 5, 6}, {6, 6, 4}, {6, 6, 5}, {6, 6, 6}}
Comments
Post a Comment