1
$\begingroup$

This question already has an answer here:

Problem: A person spends 15 dollars at the store. Eggs cost 1 dollar. Milk costs 2 dollars and bread costs 3 dollars.

I'm attempting to create a list of all possible integer solutions to the problem. I attempted to solve the problem with the solve function

Solve[e 1 + m 2 + b 3 == 15 && e > = 0 && m >= 0 && b >= 0, {e, m, b}, Integers]

I'm not solving this exact problem. In theory its the same thing except a lot bigger. The above code works and produces an answer, but I get stuck in the larger calculation. Should I go about solving the problem another way? If so how?

$\endgroup$

marked as duplicate by Artes, m_goldberg, J. M. will be back soon May 29 '17 at 11:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6
$\begingroup$

Looks like a good problem to use FrobeniusSolve:

FrobeniusSolve[{1,2,3},15]

{{0, 0, 5}, {0, 3, 3}, {0, 6, 1}, {1, 1, 4}, {1, 4, 2}, {1, 7, 0}, {2, 2, 3}, {2, 5, 1}, {3, 0, 4}, {3, 3, 2}, {3, 6, 0}, {4, 1, 3}, {4, 4, 1}, {5, 2, 2}, {5, 5, 0}, {6, 0, 3}, {6, 3, 1}, {7, 1, 2}, {7, 4, 0}, {8, 2, 1}, {9, 0, 2}, {9, 3, 0}, {10, 1, 1}, {11, 2, 0}, {12, 0, 1}, {13, 1, 0}, {15, 0, 0}}

$\endgroup$
  • $\begingroup$ Perfect answer. Thanks again. $\endgroup$ – Otto Omen May 29 '17 at 4:56
0
$\begingroup$

Carl Woll's answer is nothing more or less. It's hard to find such an answer like that unless you knew it.

In:

array[f_] :=  Array[f, {16, 16, 16}, 0] // Flatten[#, 2] &;
xss = array[List]
yss = array[#1 + #2 2 + #3 3 == 15 &];
Pick[xss, yss]

Out:

{{0, 0, 5}, {0, 3, 3}, {0, 6, 1}, {1, 1, 4}, {1, 4, 2}, {1, 7, 0}, {2,
   2, 3}, {2, 5, 1}, {3, 0, 4}, {3, 3, 2}, {3, 6, 0}, {4, 1, 3}, {4, 
  4, 1}, {5, 2, 2}, {5, 5, 0}, {6, 0, 3}, {6, 3, 1}, {7, 1, 2}, {7, 4,
   0}, {8, 2, 1}, {9, 0, 2}, {9, 3, 0}, {10, 1, 1}, {11, 2, 0}, {12, 
  0, 1}, {13, 1, 0}, {15, 0, 0}}
$\endgroup$

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