2

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Otto Omen
  • 51
  • 3

2 Answers2

6

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
0

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}}
webcpu
  • 3,182
  • 12
  • 17