How to write a simple but robust loop for the following example?

Suppose, I have a $100. Every time when I go to the coffee shop I spend 8.3 % of my money. So, at first when I spend 8.3% I will have (100- 0.083*100) = $91.7 remaining. The next day, I want to spend 8.3% of $91.7, after that I will have (91.7 - 0.083*91.7) = $84.0889 remaining, and so on.
So, for how many days I can use my money?

8 commentaires

"So, for how many days I can use my money?"
You don't seem to have a lower limit to how much money you need to have/spend, so surely the answer is "forever" ?
lower limit is zero. Any help?
Did you see my answer?
Jan
Jan le 3 Déc 2018
Modifié(e) : Jan le 3 Déc 2018
@madhan: I do not see your answer.
@A T: This sounds like a homework and you can write a small code, which calculates the answer. You just need a while loop and for keeping teh standards fractinal cents are rounded, e.g. by round(x, 2). Please try to write this code. Post it here if there are problems and ask a specific question.
The lower limit is zero? Of cause you can do this an infinite number of times, if you do not consider the rounding to cents. I guess the minimal value is 1 cent.
@Jan: madhan deleted his erroneous answer. Any help will be appreciated.
Oh, the joy of doing homeworks that ask to solve unrealistic problems... Or is this a trick question?
When you have $1 left, you cannot buy anything in most coffee shops. You may get lucky with $2.00 drip coffee. Below that, you'll get a bunch of baristas with puzzled looks, something like this face: o_O?
You may be able to get sample coffee beans or literally, one drip of coffee. Also, prices of coffee do not scale continuously from $8.30 to 0.0001 cent. You're be better off doing the following:
Days of visit = floor($100 / (minimum price of an item in the store))
You should give the homework answer and the real-life answer.
@Jan yes I deleted my answer since it was not the one OP was looking for.
"lower limit is zero. Any help?"
Then the answer is trivially easy: you can do this for infinite days, because you will never reach zero using the formula that you have given us.

Connectez-vous pour commenter.

 Réponse acceptée

Jan
Jan le 3 Déc 2018
Modifié(e) : Jan le 3 Déc 2018
What have you tried so far? The current information is that the lower limit is 0. Then the result is trivial: There is no limit in the number of iterations. So either change the lower limit to 1 cent or consider the question as solved. In other words: "$84.0889 remaining" is not meaningful.
At least this would be a start:
x = 100;
n = 0;
while x > 0.01 % One cent at least
...
end
Now implement subtracting 8.3%.
My suggestion of round was misleading, because rounding 0.06 after removing 8.3% results in 0.6 again, such that there is still an infinite number of iterations. I guess, the cent values are cropped by floor. To round the cent values, either do all calculations in cents, or use floor(x * 100) / 100.
The limit of 1 cent is reached on the 84th iteration.

Plus de réponses (1)

To answer, you need to know the price of the least expensive item in the coffee shop. I'm certain that the coffee shop does not sell anything for $0.01. In fact you might have trouble finding anything less than $0.50.
Anyway, like Jan said, 8.3% of 6 cents is less than half a cent, so that rounds to no money being spent, so you have to quit once you get to 6 cents because the requirement of spending 8.3% can no longer be met.
Here's a hint for a line of code you can put into the loop to print out your balance at each iteration:
fprintf('After %d purchases, you have %f left.\n', iteration, balance);
Unlike Jan, I get to 6 cents at 85 iterations, not 84. Maybe it's because he used floor() while I used round(). I think cash registers actually do use round() in the US. When I traveled in Europe it seemed everything is rounded to the nearest 5 euro cents, though I could be wrong.

5 commentaires

You are right: With floor to cent values the limit of 1 cent is reached after 84 days.
Some European countries decided to avoid the 1 and 2 cent coins and round to the nearest 5c value. The idea is, that this saves time and energy for counting and transporting a large amount of almost non-value heavy coins.
Honestly, I wish the US would get rid of the cent. They cost more to make than they're worth. No one even wants them - people won't even pick one up off the ground if they see one. And in the long run, the money paid will average out to the same amount anyway.
Feel free to send all your cents to me, or to a project, which needs the money more urgently than I do.
PH = 0.75; % penny diameter
PT = 0.06; % penny thickness
Box = [23+11/16, 11.75, 3];
Grid = floor(Box1./[PH PH PT]);
PennyPerBox = prod(Grid);
PennyValue = PennyPerBox/100;
ShippingCost = 18.90; %FlatRate USPS
MoneySent = PennyValue - Cost;
MassOfBox = PennyPerBox * 2.5 * 0.002204;
%You can send around 23250 pennies for $18.90 via USPS flat rate box, for a
%total of $213.60 monetary value left over. The mailman will not like
%carrying the 128-lb box though...
@OCDER: Thank you. I wil give the postman 2136 cents as a tip.

Connectez-vous pour commenter.

Question posée :

Att
le 3 Déc 2018

Commenté :

Jan
le 5 Déc 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by