Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Let $m,k,p$ be fixed positive integers. I want to create a table of k-tuples $(x_1,x_2,\ldots,x_k)$ comprised of solutions in positive integers to the equation below:

$$x_1+x_2+\cdots+x_k=m\quad\text{where }1\leq{}x_1,x_2,\ldots,x_k\leq{}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 $(x_1,x_2,x_3)$ such that $$x_1+x_2+x_3=16$$ where $1\leq{}x_1,x_2,x_3\leq{}6$.

Also, how would I seperately generate a table of positive integral triples $(x_1,x_2,x_3)$ for which $$x_1+x_2+x_3\geq{}16 \quad \text{and } 1\leq{}x_1,x_2,x_3\leq{}6.$$

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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}}

share|improve this answer
    
Thanks for the code and references. –  Black Milk Mar 31 '13 at 5:34
    
I don't know why but when I try to upvote your answer it gives me a -1 instead. –  Black Milk Mar 31 '13 at 6:07
    
@EdwinMontufar Strange... you just have to click "upper" arrow for up-vote. Not sure if you experiencing a bug or clicking on a wrong thing. I hope I won't go negative 25K suddenly ;) –  Vitaliy Kaurov Mar 31 '13 at 6:14
    
I'm definitely clicking that upper arrow =) –  Black Milk Mar 31 '13 at 6:23
    
@EdwinMontufar you got it now, a +1 from you, thanks. I think that -1 is from someone else. –  Vitaliy Kaurov Mar 31 '13 at 6:28
add comment

It seems to me that for your first stated problem there is a much better method than Solve or Reduce:

{m, k, p} = {16, 3, 6};
IntegerPartitions[m, {k}, Range@p]
{{6, 6, 4}, {6, 5, 5}}

If you want all permutations just use Permutations:

Join @@ Permutations /@ %
{{6, 6, 4}, {6, 4, 6}, {4, 6, 6}, {6, 5, 5}, {5, 6, 5}, {5, 5, 6}}

For your second stated problem I believe this will be more efficient than Solve or Reduce:

Join @@ Table[IntegerPartitions[x, {k}, Range@p], {x, m, k*p}]
{{6, 6, 4}, {6, 5, 5}, {6, 6, 5}, {6, 6, 6}}
share|improve this answer
add comment

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.