We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Bobby uses money on the first iteration only. So, after 'n //= c' operator there are only two numbers in use: n - number of bars/wrappers Bobby has at the beginning, m - price of the promotional bar in wrappers.

We can get rid of iterations when understanding that the chockolate bar "costs" not m wrappers, but m-1, because Bobby has one additional wrapper for every m of them.
So the promotional bars number can be calculated without 'for/while' operator, simply dividing wrapper number by m-1.

But Bobby can't spend all his wrappers. At least one wrapper remains. Thus the final formula is

result = n [bars count bought for money] + (n-1)[wrappers count except one wrapper] // (m-1)

## Chocolate Feast

You are viewing a single comment's thread. Return to all comments →

can you please explain how this works ?

So, how to solve the problem in two operators?

Bobby uses money on the first iteration only. So, after 'n //= c' operator there are only two numbers in use: n - number of bars/wrappers Bobby has at the beginning, m - price of the promotional bar in wrappers.

We can get rid of iterations when understanding that the chockolate bar "costs" not m wrappers, but m-1, because Bobby has one additional wrapper for every m of them. So the promotional bars number can be calculated without 'for/while' operator, simply dividing wrapper number by m-1.

But Bobby can't spend all his wrappers. At least one wrapper remains. Thus the final formula is

result = n [bars count bought for money] + (n-1)[wrappers count except one wrapper] // (m-1)