Facebook Chocolate Problem
“COWARDS DIE MANY TIMES BEFORE THEIR DEATHS, THE VALIANTS NEVER TASTE OF DEATH BUT ONCE….”
This has nothing to do with what I am going to talk about..!! But at least I could manage to get your attention!! Now how about the picture (problem??) below…
This problem has been making rounds of “facebook walls” and has got a lot of attention, likes, shares and comments!! Well the picture above also says that most of the people answered it wrongly. Most of us would answer it 21 but actually the answer is 22. The reason for getting wrong answer is the mistake in keeping track of the amazing offer of “a chocolate for 3 wrappers”! I too shared it on my facebook wall and thought of generalizing the “Facebook Chocolate Problem”!! Generalization here means the following three problems:
- How many chocolates can you buy with n rupees under the above condition?
- How many can you buy with n rupees under the condition that you get one chocolate for m wrappers?
- How many can you buy with n rupees under the condition that you get p chocolates for m wrappers?
We consider the first two problems together. Initially we can buy $$n$$ chocolates with $$n$$ rupees. So we are left with $$n$$ wrappers. Let $$q$$ and $$r$$ denote the quotient and remainder when $$n$$ is divided by $$m$$. So we could buy $$n+q$$ chocolates. Now we are left with $$q+r$$ wrappers. If $$q+r < m$$ then we cannot get any further chocolates and so we will be left with $$q+r$$ wrappers only. But if $$q+r > m$$ then we repeat the above process with $$q+r$$ taking the role of $$n$$.
We can easily develop this algorithm into a computer program. I have used MATLAB for this purpose. The following is an m-file to implement the above algorithm:
% Facebook Chocolate Problem
n=input(‘How many rupees do you have?’);
m=input(‘Number of wrappers needed to get 1 chocolate?’);
c=n; %c keeps track of the number of chocolates bought
q=n;
while q>m-1
r=mod(q,m);
q=(q-r)/m;
c=c+q;
q=q+r;
end
fprintf(‘n You can get a total of %d chocolates if you have %d rupees with you and you are left with %d wrappers’, c,n,q);
Now we consider the third problem where we are offered $$p$$ chocolates for $$m$$ wrappers. Of course the shopkeeper will not be “fool enough” to offer something like $$pge m$$, in which case the process of getting chocolates will continue indefinitely!! The algorithm here is same as above except that $$c=c+q$$ will be replaced by $$c=c+qp$$ and $$q=q+r$$ will be replaced by $$q=qp+r$$. The MATLAB code is given below:
% The Facebook Chocolate Problem
n=input(‘How many rupees do you have?’);
m=input(‘Number of wrappers needed for each exchange?’);
p=input(‘Number of chocolates offered in each exchange?’);
if p>=m
errordlg(‘Poor deal !!’);
break;
else
c=n; q=n;
while q>m-1
r=mod(q,m);
q=(q-r)/m;
c=c+q*p;
q=q*p+r;
end
fprintf(‘n You can get a total of %d chocolates if you have %d rupees with you and you are left with %d wrappers’, c,n,q);
end
So this was a crazy generalization of “Facebook Chocolate Problem” !!
Debashish Sharma, JRF, Department of Mathematics, NIT Silchar.