# Gumball Machine Logic Math Puzzle You walk into an antique shop and find an unusual gumball machine. On the back of the gumball machine you can set and lock five dials with the following labels:

• Percentage (0% through 100%)
• Pink (0 through 10)
• Pink+Green (0 through 10 for pink and 0 through 10 for green individually)
• Green (0 through 10)

A note below the dials reads "Fill machine with any number of pink and green gumballs. 400 gumballs maximum capacity."

On the front of the gumball machine is a slot to place a penny and a dial to turn and receive your treat.

You buy the machine and 1000 gumballs (500 green, 500 pink) and take it home. After a few days of experimenting you've learned the following:

• When the available number of pink gumballs is greater than the Percentage of the original (starting) number of pink gumballs it will release gumballs depending on the Pink+Green settings
• When the available number of pink is less than or equal to the Percentage of the original (starting) number of pink gumballs it will stop releasing pink gumballs and release green gumballs as set by the Green dial
• When all green gumballs are dispensed it will then release pink gumballs as designated by the Pink dial
• If a remainder of gumballs is left it will dispense the remainder on the next turn
• At least one gumball of either color must be released for each penny until the machine is empty. This means if P=0 then G>=1 else if G=0 then P>=1.

As an example:

• Fill the gumball machine with 140 pink and 100 green
• Set the Percentage dial to 70%
• Set the Pink+Green dial to 1-pink and 1-green
• Set the Pink dial to 1-pink
• Set the green dial to 2-green

Using the example settings the machine will release:

• 1-pink and 1-green gumball per turn until the number of pink is less than or equal to 70% of the original (starting) number of pink gumballs
• At less than or equal to 70% of the original (starting) pink gumballs it will release 2-green gumballs 0-pink gumballs until all green gumballs are gone
• When empty of green gumballs it will release 1-pink gumball per turn until the entire machine is empty

Clarification about remainders: If there is any remainder of a setting, such as 6 Pink when set to 7 Pink, it will dispense the 6 remaining Pink gumballs. Likewise if we are in the P+G phase and there is a remainder of Pink to reach the <=% it will dispense the remainder of Pink to reach the % or lower.

CAN YOU ANSWER THE FOLLOWING FOUR QUESTIONS?

1. How much money will it cost to get all the gumballs in the example?

2. How do you calculate for all possible settings?

3. If two green gumballs equal the cost of one pink gumball what are the best dial settings to make the most money?

4. If two green gumballs equal the cost of one pink gumball what are the worst dial settings make the least money?

• can we have the gumballs in pink and green please :)
– JMP
Sep 27, 2017 at 3:28
• 😀 I'll see what I can do for the one who answers all four questions first. Sep 27, 2017 at 4:17
• Welcome to Puzzling! This seems to be a pure math problem, and likely off-topic. Is it from homework or an ongoing competition?
– Deusovi
Sep 27, 2017 at 4:42
• No, not a homework problem and not from a competition. Funny thing is the Math site said it might be better put under puzzles and now puzzles is telling me it should go under math. It really fits under both. It's a math and logic puzzle Sep 27, 2017 at 4:49
• Odd. So the percentage is a one-time thing. Wouldn't the problem be more interesting if the first rule applied whenever the ratio of pink to green is greater than the Percent setting? Sep 27, 2017 at 7:33

(I've only spoilered the answer to the first question because the answers to the others were a bit too lengthy.)

1)

(I initially understood the percentage as referring to what percentage of all gumballs left in the machine are pink, but the OP's comment below the question indicates that it actually refers to the percentage of the original number of pink gumballs that are currently left, so I'm going with that interpretation.)

The machine begins by dispensing 1 pink and 1 green gumball for each penny, and continues to do this until there are 70% of the original number of pink gumballs left. 70% of 140 is 98, so this phase continues for 140 - 98 = 42 rounds, and so costs 42 pennies.

Then,

the machine then enters its next phase, wherein it dispenses 2 green gumballs for each penny, and continues to do this until there are no green gumballs left. The first round dispensed one green gumball per round for 42 rounds, so at the start of the second phase there are 100 - 42 = 58 green gumballs left. The second phase thus lasts for 58/2 = 29 rounds, and so costs 29 pennies.

Finally,

the machine enters its third phase, wherein it dispenses 1 pink gumball for each penny, and continues to do this until there are no pink gumballs left. After the first round, there were 98 pink gumballs left, and this remained unchanged during the second round, which dispensed no pink gumballs, so the third phase will last for 98 rounds, and so cost 98 pennies.

So,

Putting all the phases together, we have 42 + 29 + 98 pennies to release all the gumballs, which is 169 pennies.

2)

Let $$N_p$$ be the initial number of pink gumballs, $$N_g$$ be the initial number of green gumballs, $$P$$ be the percentage setting, $$D_{p1}$$ be the number of pink dispensed per penny in the pink-and-green phase, $$D_{g1}$$ be the number of green dispensed per penny in the pink-and-green phase, $$D_{p2}$$ be the number of pink dispensed per penny in the pink-only phase, and $$D_{g2}$$ be the number of green dispensed per penny in the green-only phase.

If $$N_p$$ is 0, presumably the machine will skip the pink-and-green phase, go straight into the green-only phase, continue till all green gumballs are dispensed, and then skip the pink-only phase as well. In this case, the number of rounds (and thus pennies), which we will denote by $$R_2$$, will either be the largest whole number such that $$D_{g2}$$ multiplied by that number is less than $$N_g$$ (sorry, I'm pretty sure there's a name for that, but my maths was a long time ago and I've forgotten it! Let's call it $$L$$.), or $$L+1$$. If $$N_g$$ is a multiple of $$D_{g2}$$, it will be $$L$$, and if not then it will be $$L+1$$.

If $$N_g$$ is 0, then (if $$D_{g1}>0$$) it is not entirely clear from the rules what happens: does the machine skip straight to the pink-only phase, or does it dispense the appropriate number of pink gumballs each turn for the pink-and-green phase, even though it cannot accompany them with the appropriate number of green gumballs, until the percentage of pink gumballs is reached? Assuming the former, the number of rounds and thus pennies (which we will denote by $$R_3$$) can be calculated in the same way as for the case where $$N_p$$ was 0, replacing all the $$g$$s with $$p$$s.

If $$N_g$$ and $$N_p$$ are both non-zero, then the machine starts in the pink-and-green phase. It is known that this will terminate when the number of pink gumballs remaining drops below $$P$$% of $$N_p$$. As already mentioned, it is not clear from the rules whether it also terminates when the number of green gumballs reaches 0, but we will assume that it does.

This issue does not arise in any case when $$N_g/D_{g1}$$ is greater than $$100-P$$% of $$N_p$$ divided by $$D_{p1}$$. In this case, the pink-and-green phase continues for $$R_1$$ rounds, where $$R_1$$ is the smallest number such that $$R_1\times D_{p1}>(1-P/100)\times N_p$$. At the end of the first phase, there are $$N_p - (R_1 \times D_{p1})$$ pink and $$N_g - (R_1 \times D_{g1})$$ green gumballs left.

If $$N_g/D_{g1}$$ is less than or equal to $$100-P$$% of $$N_p$$ divided by $$D_{p1}$$, then the number of rounds in the pink-and-green phase, which we will again denote $$R_1$$, is $$N_g/D_{g1}$$ if that is a whole number, and otherwise it is 1 + the largest number which when multiplied by $$D_{g1}$$ gives a value less than $$N_g$$.

If there are any green gumballs left after the first phase (i.e. if $$N_g/D_{g1}$$ is greater than $$100-P$$% of $$N_p$$ divided by $$D_{p1}$$), then the machine enters the green-only phase for a number of rounds which we will denote by $$R_2$$. If there are no green gumballs left after the first phase (i.e. if $$N_g/D_{g1}$$ is less than or equal to $$100-P$$% of $$N_p$$ divided by $$D_{p1}$$) then $$R_2=0$$.

If the machine does enter the green-only phase, then it will do so with $$N_g - (R_1 \times D_{g1})$$ green gumballs. For simplicity, let us refer to this number as $$N_{g2}$$. If $$N_{g2}/D_{g2}$$ is a whole number, then $$R_2=N_{g2}/D_{g2}$$. Otherwise, $$R_2$$ is 1 greater than the largest number such that $$D_{g2}$$ multiplied by that number is less than $$N_{g2}$$.

Finally, the machine will enter the pink-only phase, for $$R_3$$ rounds, provided that $$N_p - (R_1 \times D_{p1}) > 0$$ (if that is not the case then $$R_3$$ will be 0), and there will be $$N_p - (R_1 \times D_{p1})$$ pink gumballs at the start of this round. Let us call this number $$N_{p2}$$ for simplicity. $$R_3$$ can now be calculated in the same way as $$R_2$$ was, replacing $$g$$ by $$p$$ throughout.

The total number of pennies is then given by $$R_1+R_2+R_3$$.

3) and 4)

The price that you pay for the gumballs has no bearing on the dial-setting strategy. This is because throughout we have been assuming that the customer (or a steady stream of different customers) will keep inserting pennies regardless of how attractive to them the offer of a certain number of gumballs of a certain colour or colours is. Unlike in the real world, the fact that pink gumballs cost you twice as much as green gumballs do has no implications for how you should price them in the machine, because you don't have to worry that the customers won't go for inferior green gumballs offered at the same price as the pink ones, and you can just go ahead and set the $$D_{p2}$$ and $$D_{g2}$$ both to 1, and $$P$$ to 100, so that the machine enters the green-only phase right from the beginning, followed by the pink-only phase, and only ever dispenses one gumball for a penny; it does not matter what you set $$D_{p1}$$ and $$D_{g1}$$ to since these dials will never come into play. (This must be the strategy that maximises your revenue, since each turn must dispense at least one gumball, so there is no room for improvement.)

Similarly, for the least revenue, you need to maximise the number of gumballs released every turn, and if (as shown in the illustration) the dials governing the number of gumballs released all have the same maximum, then the way to ensure the most gumballs are released per turn is to have the machine stay in the pink-and-green phase throughout, so that you get the maximum number of each of two colours instead of the maximum number of just one, and so you must set $$P$$ to 0 and $$D_{p1}$$ and $$D_{g1}$$ both to their maximum possible values. (It does not matter what you set $$D_{g2}$$ and $$D_{p2}$$ to.)

The only thing you can change about your strategy which is determined by the cost to you of the gumballs is the number of pink and green balls you fill it with at the start, and the optimal and pessimal strategies are trivial: the optimal strategy is to fill it entirely with whichever colour is cheaper, and the pessimal to fill it with whichever is more expensive.

To summarise, your profit is how much more money you get from people inserting pennies into the machine than you spent in stocking it. You maximise profit by a) maximising the revenue from the machine, which means maximising the number of turns it takes to empty it, which is unrelated to how much each gumball costs you when stocking it, and b) minimising the amount you spend on stocking the machine, which does depend on the relative cost to you of the different colours of gumball.

• I just started reading your fantastically detailed answer and I'm stuck on the "state in which there are still gumballs inside it, but no further turns will dispense any gumballs." I just updated the rules to clarify a few things and one is that at least one gumball must be released per turn until the machine is empty. How does this change your answer, or am I just missing something? I'll continue reading to see! I appreciate everyone's effort in this puzzle! Sep 28, 2017 at 21:47
• Yes, that was because at the time I wrote the answer, the rules weren't clear as to what would happen in the case where the machine was supposed to dispense some number of gumballs of a particular colour on the next turn, but there were fewer than that number remaining in it. For example, in the pink-only state, if it was supposed to dispense 7 per penny but there were only 6 left: would it dispense those 6 on the next turn, or go into a sulk and refuse to dispense anything further since it couldn't dispense the whole 7. (The rules explained that if a single gumball was left, it would (cont) Sep 28, 2017 at 22:15
• (cont) be dispensed on the next turn, but didn't deal with the case where more than one remained.) You've now clarified that at least one gumball must be dispensed per turn till the machine is empty, which rules out the possibility of it reaching a state where it refuses to dispense anything before it's actually empty, but I think it's still not quite clear exactly what does happen: supposing it is in the pink-only state, with 6 pink gumballs left, and it's supposed to dispense 7 per penny, will it dispense all 6 on the next turn, dispense the remaining balls one by one over the next 6 (cont) Sep 28, 2017 at 22:20
• (cont) turns, or something else? (The first option seems most logical, but is not actually specified.) What about if it's in the pink-and-green state, with only 3 green gumballs left but it's supposed to dispense 4 (and some number of pink) on the next turn? Does it dispense the correct number of pink and all the remaining green, then move to the pink-only state? Or move straight to the green-only state? Does it make a difference whether that turn would have brought the pink gumballs under the percentage threshold? Sep 28, 2017 at 22:25
• I see the issue I've created here... If there is any remainder of a setting, such as 6 when set to 7, it will dispense 6 remainder. Likewise if we are in the P+G phase and there is a remainder of Pink to reach the <=% it will dispense the remainder of Pink to reach the % or lower. I'll clarify that as soon as I have access to a computer. My cell phone thumb typing skills are lower than the set %. 😉 Sep 28, 2017 at 23:20

I wrote a SQL function to answer part 2 of the question. Try it here.

Essentially, it takes as input the number of pink gumballs in the machine, the number of green gumballs in the machine, the percent (as a decimal), the number set on the first phase pink dial, the number set on the first phase green dial, the number set on the second phase green dial, and the number set on the third phase pink dial.

It's obviously not gonna find the answer to 3 and 4, but it's an accurate answer to 2.

NB: To run the function, simply replace the numbers in the field on the right with the appropriate values, then press "Run SQL."

Edit: CODE

create function Gumballs(@Pink int, @Green int,
@pct numeric(3,2),
@PinkPGPhase int, @GreenPGPhase int,
@PinkDialPhase int, @GreenDialPhase int)
returns int as
begin

declare @result int = 0
declare @PinkStart int = @Pink

While @Pink >= @pct*(@PinkStart)
BEGIN
set @result+=1
set @Pink-=@PinkPGPhase
set @Green-=@GreenPGPhase
END

WHILE @Green > 0
BEGIN
set @Green-=@GreenDialPhase
set @result+=1
END

WHILE @Pink > 0
BEGIN
set @Pink-=@PinkDialPhase
set @result+=1
END

RETURN @result

end


To run it, here's the query:

SELECT dbo.Gumballs(140, --pink gumballs
100, --green gumballs
.7,  --percentage
1,   --pink/green shared phase--pink dial
1,   --pink/green shared phase--green dial
1,   --pink alone phase dial
2)   --green alone phase dial


This returns 169.

• Can you put the code and results in your answer? (The link might break at some time) Sep 28, 2017 at 5:03
• Suuuuuuuuuuure. Sep 28, 2017 at 15:49
• This certainly works as a solution for the second question (and the first) under the interpretation of the rules that I originally had, but it's now been clarified that that interpretation is incorrect: the pink-and-green phase stops not when the number of pink balls remaining falls below a certain percentage of the total remaining balls, but when the number of pink balls remaining falls below a certain percentage of the original number of pink balls. Sep 29, 2017 at 1:42
• Thanks @Neremanth! I've updated my answer to take that into account. Sep 29, 2017 at 15:20
• you need >= otherwise phase 1 never happens.
– JMP
Sep 29, 2017 at 16:58

Question 1 is straight-forward:

30% of 140 is 42 in PG phase, then 58/2=29 in G phase, and then 140-42=98 in P phase. Total 140+29=169.

A JavaScript calculator for question 2:

JSFiddle gumball calculator

<input type='number' id='pinit' value='140'/><label for='pinit'>Pink start</label><br>
<input type='number' id='ginit' value='100'/><label for='ginit'>Green start</label><br>
<input type='number' id='per' value='70'/><label for='per'>Percentage</label><br>
<input type='number' id='pgp' value='1'/><label for='pgp'>Pink+Green Pink</label><br>
<input type='number' id='pgg' value='1'/><label for='pgg'>Pink+Green Green</label><br>
<input type='number' id='g' value='2'/><label for='g'>Green</label><br>
<input type='number' id='p' value='1'/><label for='p'>Pink</label><br>

<button onclick='calculate();'>Calculate</button>
<br>
<span id='outpg'></span> in pink and green phase<br>
<span id='outg'></span> in green phase<br>
<span id='outp'></span> in pink phase<br>
Total : <span id='output'></span>

<script>

function calculate() {
var res=0;
var pLeft=pinit.value;
var gLeft=ginit.value;

nPG=Math.ceil(pinit.value*(100-per.value)/(pgp.value*100));
pLeft-=nPG*pgp.value;
gLeft-=nPG*pgg.value;
res=nPG;
outpg.textContent=nPG;
chgG=Math.ceil(gLeft/g.value);
res+=chgG;
outg.textContent=chgG;
chgP=Math.ceil(pLeft/p.value);
res+=chgP;
outp.textContent=chgP;
output.textContent=res;
}
</script>

• My calculations go like this: start with 140 pink and 100 green. You get one pink and one green for 70 rounds at which point you have 70 pink and 30 green. This is the 70% mark when the distribution changes. Now you get 2 green until all the green are gone: 30/2 = 15. Lastly, you get 1 pink per turn: 70. Total: 70+15+70=155. Sep 27, 2017 at 10:49
• For 1 it's the example settings, right? There's no optimization. You either get the answer right or wrong. I'm not sure how you interpreted the example. Sep 27, 2017 at 10:56
• OP doesn't say what the 70% is of - i went for initial number of pinks
– JMP
Sep 27, 2017 at 10:57
• in yours, after you release 1 green gumball, the percentage is above 70% again, so what happens? do we go back to PG phase? which sounds very silly to me.@HughMeyers
– JMP
Sep 27, 2017 at 11:00
• All, thank you very much for identifying issues, I greatly appreciate it. I'll work to clarify the original question during my lunch break. Lets say the machine is filled with 100 P gumballs at the beginning of the day and the percentage is set to 70. the machine will release P+G until there are only 70 P available. At this point it will go to the next step of only releasing G. Sep 27, 2017 at 21:07