## 17 Dec 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 chocolates with rupees. So we are left with wrappers. Let and denote the quotient and remainder when is divided by . So we could buy chocolates. Now we are left with wrappers. If then we cannot get any further chocolates and so we will be left with wrappers only. But if m" /> then we repeat the above process with taking the role of .

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 chocolates for wrappers. Of course the shopkeeper will not be “fool enough” to offer something like , in which case the process of getting chocolates will continue indefinitely!! The algorithm here is same as above except that will be replaced by and will be replaced by . 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.*

Featured Image Courtesy : Shutterstock

#### Debashish Sharma

#### Latest posts by Debashish Sharma (see all)

- Algebra behind the scenes : Reed-Solomon Algorithm - April 29, 2017
- In memory of Prof. Jonathan Borwein - August 7, 2016
- "Mathematics Lab" - July 20, 2016

## Rahul

Posted at 16:52h, 19 DecemberNice Bro(dishari ka..:P)... Good Job... Keep it up..:)

## Gonit Sora

Posted at 21:21h, 20 DecemberThanks.

## Debashish

Posted at 17:04h, 26 December@Rahul ..... 😛 ... thanks....