Omnimaga

General Discussion => Other Discussions => Math and Science => Topic started by: thepenguin77 on June 25, 2012, 11:49:00 pm

Title: Least amount of change
Post by: thepenguin77 on June 25, 2012, 11:49:00 pm
Now this might be a rather simple problem, but I don't think it is. I'm asking this partially because I'm too lazy to figure it out right now, and also because I feel it will give people an opportunity to think. (This might even end up requiring a program to solve)


So, no one likes to carry around change (coins) and my question is, what coins should you enter a store with so that sum of the number of coins you enter a store and leave a store with is at a minimum?

The general idea here is that you basically want to carry the least amount of change. So, the way this works, is you pick E number of coins to enter with, after buying your items (which have random number of cents) you leave with L number of coins. You want to minimize L + E.

Rules:



Have at it. You'll of course have to provide some sort of justification for your answer.

Spoiler For accepted answer:
Well, it looks like runer had it right from the start. Carrying 0 coins really is the best option.

I believe the solution is to carry no change.

Whatever the cost of your purchase is and whatever coins you have, you can ensure you leave with the minimum amount of change possible by giving the vendor all of your change. Whatever coins properly contribute to the payment, the vendor will keep; the coins that do not will be returned to you. Since the cents amount of the purchase is an equally-distributed random number, subtracting your constant amount of change from this will result in another equally-distributed random number (mod 100). Whatever amount of change you contribute, the vendor will always have to return to you coins that sum to an equally-distributed random amount of cents from 0-99.

You cannot limit the amount of coins you end with, so the optimal solution is simply to limit the amount of coins you begin with.

And with 2 brute force checks to back this, I'm happy. It looks like I've been doing it right all along.
Title: Re: Least amount of change
Post by: ruler501 on June 25, 2012, 11:54:59 pm
isnt it 4 pennies one nickel two dimes and 3 quarters since you can make any change out of that combination

for 1-4 cents use the pennies
for 5-9 use one nickel and the neccesary pennies
for 10-19 use 1  dime and the pennies/nickel for the remainder
for 20-24 use two dimes and pennies
for 25-29 use quarter and pennies
for 30-34 use quarter, nickel and pennies
for 35-39 use quarter, dime and pennies
etc.

and i have 3 half dollars if you look for them they are still rare but not too hard to find a few
I also have a dollar coin and thats pretty rare or at least used to be
Title: Re: Least amount of change
Post by: thepenguin77 on June 25, 2012, 11:58:43 pm
Yes, that's how you make a number. But the idea here is that there is a purchase in the middle.

Basically, I want to have a certain set of coins in my pocket when entering a store so that when I leave, I will also have a small amount of change.

Let's say the correct answer is a quarter and a dime. I would then have to show that 2 + (all of the possible numbers of coins I could be holding when leaving) is more optimized for my quarter-dime scenario than any other set of entering coins.
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 12:02:27 am
depends is it just when you leave that you count coin or the average of when you left/entered?
Title: Re: Least amount of change
Post by: someone on June 26, 2012, 12:19:20 am
So, no one likes to carry around change (coins) and my question is, what coins should you enter a store with so that sum of the number of coins you enter a store and leave a store with is at a minimum?

The general idea here is that you basically want to carry the least amount of change. So, the way this works, is you pick E number of coins to enter with, after buying your items (which have random number of cents) you leave with L number of coins. You want to minimize L + E.

Rules:
  • We're using American coins, so the choices are: penny - .01, nickel - .05, dime - .10, quarter - .25 (no half dollars, too rare :P)
  • The number of cents your purchase costs is random (So, a purchase would cost some dollar amount + [0 - 99] cents)
  • You receive the minimum number of coins from the cashier ($.90 is not 9 dimes)
You enter with no coins & you leave with no coins, and that is the least amount of change you have to bring. If you want to buy something, use a credit card ;)
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 12:21:21 am
So, no one likes to carry around change (coins) and my question is, what coins should you enter a store with so that sum of the number of coins you enter a store and leave a store with is at a minimum?

The general idea here is that you basically want to carry the least amount of change. So, the way this works, is you pick E number of coins to enter with, after buying your items (which have random number of cents) you leave with L number of coins. You want to minimize L + E.

Rules:
  • We're using American coins, so the choices are: penny - .01, nickel - .05, dime - .10, quarter - .25 (no half dollars, too rare :P)
  • The number of cents your purchase costs is random (So, a purchase would cost some dollar amount + [0 - 99] cents)
  • You receive the minimum number of coins from the cashier ($.90 is not 9 dimes)
You enter with no coins & you leave with no coins, and that is the least amount of change you have to bring. If you want to buy something, use a credit card ;)
cheating :P

I think we agreed on irc that you enter with none so that the max you will ever have is 10(see my first reply for why)
Title: Re: Least amount of change
Post by: Juju on June 26, 2012, 12:24:43 am
That's cheating.

Let's break up in denomination of less than $1, 25¢, 10¢ and 5¢, respectively.

So to make anything less than $1, you'll need 3 quarters max.
Less than 25¢: 2 dimes max.
Less than 10¢: 1 nickel max.
Less than 5¢: 4 pennies max.

So 3 quarters, 2 dimes, 1 nickel and 4 pennies will make anything 1-99¢. The amount of coins when you enter will be at minimum and when you go out will also be at minimum since the cashier won't give you any change.

Or else you can carry one $1 coin (we have that in Canada, dunno in US) and the cashier will give minimum change.
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 12:31:11 am
That's cheating.

Let's break up in denomination of less than $1, 25¢, 10¢ and 5¢, respectively.

So to make anything less than $1, you'll need 3 quarters max.
Less than 25¢: 2 dimes max.
Less than 10¢: 1 nickel max.
Less than 5¢: 4 pennies max.

So 3 quarters, 2 dimes, 1 nickel and 4 pennies will make anything 1-99¢. The amount of coins when you enter will be at minimum and when you go out will also be at minimum since the cashier won't give you any change.

Or else you can carry one $1 coin (we have that in Canada, dunno in US) and the cashier will give minimum change.
i say this is the same as my original answer event though i didnt explain all of it or understand the problem ;)
Title: Re: Least amount of change
Post by: Runer112 on June 26, 2012, 12:38:10 am
I believe the solution is to carry no change.

Whatever the cost of your purchase is and whatever coins you have, you can ensure you leave with the minimum amount of change possible by giving the vendor all of your change. Whatever coins properly contribute to the payment, the vendor will keep; the coins that do not will be returned to you. Since the cents amount of the purchase is an equally-distributed random number, subtracting your constant amount of change from this will result in another equally-distributed random number (mod 100). Whatever amount of change you contribute, the vendor will always have to return to you coins that sum to an equally-distributed random amount of cents from 0-99.

You cannot limit the amount of coins you end with, so the optimal solution is simply to limit the amount of coins you begin with.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 01:10:44 am
I did the math, and if you carry in no coins at all, you will leave on average with 4.7 coins.  However, if you carry with you 1 penny, you will be able to spend it 4 out of 5 times, reducing your average to 4.11 coins.  This is not a solution, just shows that the solution is not to carry no coins.
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 01:16:34 am
time to brute force it :P
I could write a program to do that but i think someone should make the proof first of what is best
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 01:44:30 am
The answer is finding the least amount of change that allows you to match most combinations possible.

You'll need 4 pennies so you can fill the gap between nickels.
Adding in 2 nickels and 1 dime lets you cover all values from 1-24.
It follows that adding in 3 quarters would then cover all values from 1-99
So the following combination is the least amount of change you should carry:

4 pennies
2 nickels
1 dime
3 quarters

Since the distribution of purchase costs are random in this scenario, it follows that on average, half of your coins will be used (or at least close to that).

Actually, this would be the solution if you wanted to minimize the amount of change you received. But this isn't the solution to this particular problem. Using the solution presented above coupled with the fact that the cashier will always present the least amount of change, that means you will receive on average 5 coins. Knowing this means that you also know the probability of receiving a particular coin.

pennies: 40%
nickels: 20%
dime: 10%
quarters: 30%

So you want to select 5 coins with the highest probability.
In that case, you would select 4 pennies and 1 quarter.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 02:03:38 am
nitacku I already showed that if you carry *no* coins, you will receive 4.7 coins back on average, so if you are going to be carrying in any coins at all, you will have to carry less than 5 at the very least.

EDIT: And i revoke my previous decision that carrying no change was not the best solution, I was doing bad math :P I know think that it is indeed the best solution.

EDIT2:  I un-revoke my previous decision.  For some reason I was under the impression that you had to spend all of your input change but that is untrue!  There are cases where *not* spending additional coins can actually save you coins.  For example if a product cost 1.00 and you gave them 2.00 instead of 2.01, you would get no coins as change instead of 9 coins.  Now to actually set up code to have the program give the optimal number of change...
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 02:14:15 am
Yes you are indeed correct Builderboy. However, 4.7 coins is not an integer amount. So carrying 5 coins is still the optimal solution.

Here's my thoughts:
You know you will get 4.7 coins back on average, which is 5 coins when viewed as an integer amount.
To minimize your change, you need to predict what 5 coins those will be on average.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 02:17:59 am
That logic does not follow.  If one person receives an average of 4.7 coins every transaction, and another person receives an average of 5 coins per transaction, the person with 5 coins will wind up with more coins overall, even though he would have been getting integer amounts of coins each time.

EDIT: plus, even if you pick 5 optimized coins, you will still be getting some coins back on average, and including your starting coins your average will still be far above 4.7
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 02:18:36 am
or you bring 3/4 that will reduce what you get back
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 02:24:27 am
For the sake of argument, lets suppose you carried with you the exact change you needed:

Carrying 4 coins means you will have 0.7 coins too little.
Carrying 5 coins means you will have 0.3 coins too much.

Since you're trying to minimize both sides, the smallest value is the one to select.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 02:26:13 am
How can you be carrying exact change and still not have enough or have to much?

And the simple math boils down to the fact that you cannot have an average less than 4.7 coins by carrying 5 coins. 
Title: Re: Least amount of change
Post by: Wretchedlout on June 26, 2012, 02:29:03 am
The answer is 9 coins, assuming the object being bought is 1-99 cents
That is because the thing requiring the most amount of coins is 94 cents which is done with the least ammount of coins which is 3Q 1D 1N 4P
Or 9 coins.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 02:30:04 am
The answer is 9 coins, assuming the object being bought is 1-99 cents
That is because the thing requiring the most amount of coins is 94 cents which is done with the least ammount of coins which is 3Q 1D 1N 4P
Or 9 coins.

Erm, you might want to re-read the original post, as you answered a different least-change question :P
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 02:30:17 am
Ah, you're correct. It isn't exact change, but an exact subset of the change needed. In some cases it will be exact, in others too much or too little.

EDIT:
The question is: How do you carry 4.7 coins?
You can't. But you can carry 5 coins. And carrying 4 coins means more often than not you wont have enough.
Knowing this, simply choosing the most probably occurring coins will yield the best result.
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 02:31:35 am
So we do know that the answer is less than 5 coins, but whether that answer is not zero is yet to be determined.  It gets a bit more complex since you can give the cashier any combination of coins to help minimize the change you get back.
Title: Re: Least amount of change
Post by: Wretchedlout on June 26, 2012, 02:35:33 am
What are the 5 coins?
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 02:38:07 am
Well based on the probability of receiving a particular coin: 4 pennies and 1 quarter.

You wouldn't choose 5 pennies since that would be less efficient (aka nickel)
The quarter being the next most probable coin is then selected.


EDIT: I think I follow what you're saying now Builderboy.
You can't choose more than 4.7 coins since that is the average received.
Choosing more would be a less-than-optimal solution than just carrying none.

The solution may be more difficult than this, but right now I think 4 pennies is the optimal solution.
Title: Re: Least amount of change
Post by: Wretchedlout on June 26, 2012, 02:59:44 am
So I brute forced some numbers:
For each amount of cents (1-99) I use the least amount of change for each
Like if the cent value was 21 I would have 2dimes and a penny...etc
So if you add up all the times each coin was used
Quarter 150 times
Dime 80 times
Nickel 40 times
Penny 2832 times
%
4.84
2.58
1.29
91.30

Yup I think 4 pennies
Title: Re: Least amount of change
Post by: Builderboy on June 26, 2012, 10:08:24 am
Erm I think your value for pennies is off.  There are only 100 different cases, and you will only be getting back at maximum 4 pennies per transaction.  So that is a *maximum* of 400, much lower than 2832.  The actual number of pennies is actually half of 400 though, since on average, you get back 2 pennies per transaction.
Title: Re: Least amount of change
Post by: thepenguin77 on June 26, 2012, 12:52:57 pm
Well, I wrote a program to do it. This program stepped through all of the possible store-enter values and made the least amount of change for them to enter the store with (So 21 is penny, dime, dime) Then, it took each of those coin sets, and bought all the items from [0-99] cents and kept track of what was returned. Here are the results from this:

Spoiler For Spoiler:


enter value
        enter+leave
                enter
                        leave
0       4.7     0       4.7
1       5.7     1       4.7
2       6.7     2       4.7
3       7.7     3       4.7
4       8.7     4       4.7
5       5.7     1       4.7
6       6.7     2       4.7
7       7.7     3       4.7
8       8.7     4       4.7
9       9.7     5       4.7
10      5.7     1       4.7
11      6.7     2       4.7
12      7.7     3       4.7
13      8.7     4       4.7
14      9.7     5       4.7
15      6.7     2       4.7
16      7.7     3       4.7
17      8.7     4       4.7
18      9.7     5       4.7
19      11      6       4.7
20      6.7     2       4.7
21      7.7     3       4.7
22      8.7     4       4.7
23      9.7     5       4.7
24      11      6       4.7
25      5.7     1       4.7
26      6.7     2       4.7
27      7.7     3       4.7
28      8.7     4       4.7
29      9.7     5       4.7
30      6.7     2       4.7
31      7.7     3       4.7
32      8.7     4       4.7
33      9.7     5       4.7
34      11      6       4.7
35      6.7     2       4.7
36      7.7     3       4.7
37      8.7     4       4.7
38      9.7     5       4.7
39      11      6       4.7
40      7.7     3       4.7
41      8.7     4       4.7
42      9.7     5       4.7
43      11      6       4.7
44      12      7       4.7
45      7.7     3       4.7
46      8.7     4       4.7
47      9.7     5       4.7
48      11      6       4.7
49      12      7       4.7
50      6.7     2       4.7
51      7.7     3       4.7
52      8.7     4       4.7
53      9.7     5       4.7
54      11      6       4.7
55      7.7     3       4.7
56      8.7     4       4.7
57      9.7     5       4.7
58      11      6       4.7
59      12      7       4.7
60      7.7     3       4.7
61      8.7     4       4.7
62      9.7     5       4.7
63      11      6       4.7
64      12      7       4.7
65      8.7     4       4.7
66      9.7     5       4.7
67      11      6       4.7
68      12      7       4.7
69      13      8       4.7
70      8.7     4       4.7
71      9.7     5       4.7
72      11      6       4.7
73      12      7       4.7
74      13      8       4.7
75      7.7     3       4.7
76      8.7     4       4.7
77      9.7     5       4.7
78      11      6       4.7
79      12      7       4.7
80      8.7     4       4.7
81      9.7     5       4.7
82      11      6       4.7
83      12      7       4.7
84      13      8       4.7
85      8.7     4       4.7
86      9.7     5       4.7
87      11      6       4.7
88      12      7       4.7
89      13      8       4.7
90      9.7     5       4.7
91      11      6       4.7
92      12      7       4.7
93      13      8       4.7
94      14      9       4.7
95      9.7     5       4.7
96      11      6       4.7
97      12      7       4.7
98      13      8       4.7
99      14      9       4.7



So, as you can see, you will always leave with on average 4.7 coins meaning that taking in anything is just a waste.

The method I used for purchases was to pay with the least valued coins first. This means use all the pennies, then all the nickels, dimes, quarters, and finally, break out a dollar if need be. This method should maximize the change you give away because the reverse operation (starting big and going to small) minimizes the change you pay. Apparently using this strategy, you'll always end up with the same net coins.

Finally, my program didn't check the non-standard coin sets (like 3 dimes) but, since these all return with 4.7, I assume they will not break the trend.
Title: Re: Least amount of change
Post by: someone on June 26, 2012, 03:42:59 pm
what coins should you enter a store with so that sum of the number of coins you enter a store and leave a store with is at a minimum?

The general idea here is that you basically want to carry the least amount of change. So, the way this works, is you pick E number of coins to enter with, after buying your items (which have random number of cents) you leave with L number of coins. You want to minimize L + E.

Rules:
  • We're using American coins, so the choices are: penny - .01, nickel - .05, dime - .10, quarter - .25 (no half dollars, too rare :P)
  • The number of cents your purchase costs is random (So, a purchase would cost some dollar amount + [0 - 99] cents)
  • You receive the minimum number of coins from the cashier ($.90 is not 9 dimes)



Have at it. You'll of course have to provide some sort of justification for your answer.
I'm not sure I get the question right or not (and all the rules).

Are you asking which coins you should bring for any situation? (in which the cost of the purchase varies from 0 to 99 cents, excluding round numbers which you can therefore use bills). If so, then you would you be able to use dollars. Because it would be useful to use them when the price is between 51¢ & 99¢, because then you would only need to calculate from 0¢ to 50¢, because the other part is just the complement (e.g. 66¢ = $1 - 34¢, so you calculate over 34¢ & not the 66¢).

I was bored, so I was playing around with an Excel sheet & count that at most you would need for all & any case the following coins:
3 pennies
1 nickel
2 dimes
2 quarters

However, if the question is just which is the worst of the cases, then you'll need to bring 5 coins at most, but it varies depending on the case...
Spoiler For Spoiler:
¢   1   5   10   25      1   5   10   25      coins
0   0   0   0   0      0   0   0   0      0
1   1   0   0   0      0   0   0   0      1
2   2   0   0   0      0   0   0   0      2
3   3   0   0   0      0   0   0   0      3
4   0   1   0   0      1   0   0   0      2
5   0   1   0   0      0   0   0   0      1
6   1   1   0   0      0   0   0   0      2
7   2   1   0   0      0   0   0   0      3
8   0   0   1   0      2   0   0   0      3
9   0   0   1   0      1   0   0   0      2
10   0   0   1   0      0   0   0   0      1
11   1   0   1   0      0   0   0   0      2
12   2   0   1   0      0   0   0   0      3
13   3   0   1   0      0   0   0   0      4
14   0   1   1   0      1   0   0   0      3
15   0   1   1   0      0   0   0   0      2
16   1   1   1   0      0   0   0   0      3
17   2   1   1   0      0   0   0   0      4
18   0   0   2   0      2   0   0   0      4
19   0   0   2   0      1   0   0   0      3
20   0   0   0   1      0   1   0   0      2
21   1   0   2   0      0   0   0   0      3
22   2   0   2   0      0   0   0   0      4
23   0   0   0   1      2   0   0   0      3
24   0   0   0   1      1   0   0   0      2
25   0   0   0   1      0   0   0   0      1
26   1   0   0   1      0   0   0   0      2
27   2   0   0   1      0   0   0   0      3
28   3   0   0   1      0   0   0   0      4
29   0   1   0   1      1   0   0   0      3
30   0   1   0   1      0   0   0   0      2
31   1   1   0   1      0   0   0   0      3
32   2   1   0   1      0   0   0   0      4
33   0   0   1   1      2   0   0   0      4
34   0   0   1   1      1   0   0   0      3
35   0   0   1   1      0   0   0   0      2
36   1   0   1   1      0   0   0   0      3
37   2   0   1   1      0   0   0   0      4
38   3   0   1   1      0   0   0   0      5
39   0   0   0   2      1   0   1   0      4
40   0   0   0   2      0   0   1   0      3
41   1   0   0   2      0   0   1   0      4
42   2   1   1   1      0   0   0   0      5
43   0   0   0   2      2   1   0   0      5
44   0   0   2   1      1   0   0   0      4
45   0   0   2   1      0   0   0   0      3
46   1   0   2   1      0   0   0   0      4
47   0   0   0   2      3   0   0   0      5
48   0   0   0   2      2   0   0   0      4
49   0   0   0   2      1   0   0   0      3
50   0   0   0   2      0   0   0   0      2

Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 08:50:53 pm
I wrote a C program to brute force the result since I'm lazy :P

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include "SFMT.c"

typedef struct ChangeStruct
{
int penny;
int nickel;
int dime;
int quarter;
} Change;

Change min_change(int value);

int main()
{
Change carry;
Change purchase;
Change exchange;
Change best_change;
int transactions = 10000000;
double best_result = 5;
double average;

int penny = 4;

for (; penny >= 0; penny--)
{
int nickel = 1;

for (; nickel >= 0; nickel--)
{
int dime = 2;

for (; dime >= 0; dime--)
{
int quarter = 3;

for (; quarter >= 0; quarter --)
{
int total_coins = 0;
int x = 0;

carry.penny = penny;
carry.nickel = nickel;
carry.dime = dime;
carry.quarter = quarter;

init_gen_rand(time(NULL));

for (; x<transactions; x++)
{

purchase = min_change(gen_rand32()%100);

exchange.penny = abs(purchase.penny - carry.penny);
exchange.nickel = abs(purchase.nickel - carry.nickel);
exchange.dime = abs(purchase.dime - carry.dime);
exchange.quarter = abs(purchase.quarter - carry.quarter);

total_coins += exchange.penny + exchange.nickel + exchange.dime + exchange.quarter;
}

average = (double)total_coins/transactions;

printf("Pennies: %d Nickels: %d Dimes: %d Quarters: %d\n", penny, nickel, dime, quarter);
printf("Average coins per transaction: %f\n\n", average);

if (average < best_result)
{
best_result = average;
best_change = carry;
}
}
}
}
}

printf("Best Result: %f\n", best_result);
printf("Pennies: %d Nickels: %d Dimes: %d Quarters: %d\n", best_change.penny, best_change.nickel, best_change.dime, best_change.quarter);

return 0;
}

Change min_change(int value)
{
Change min_change;

min_change.penny = 0;
min_change.nickel = 0;
min_change.dime = 0;
min_change.quarter = 0;

while (value)
{
if (value >= 25)
{
value -= 25;
min_change.quarter++;
}
else
{
if (value >= 10)
{
value -= 10;
min_change.dime++;
}
else
{
if (value >=5)
{
value -= 5;
min_change.nickel++;
}
else
{
value -= 1;
min_change.penny++;
}
}
}
}

return min_change;
}

If you want to compile it you will also need the SIMD-oriented Fast Mersenne Twister library: http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/SFMT/


So the results the program generated:
Best Result: 3.198689
Pennies: 2 Nickels: 0 Dimes: 1 Quarters: 1


It's averaged over 10,000,000 transactions per combination.
Should be enough for statistical evidence.

EDIT: Best Result is the average number of coins leftover.
EDIT2: I should probably clarify, this isn't quite the answer the problem is looking for, but it is the answer to "What change should I carry to minimize my change received". But now that I think about it, even this isn't quite an answer, simply because 37 cents isn't enough to cover 38 cents, which means you'd have to pull out a $1 for that 1 cent which then makes your dollar 99 cents. This answer is merely the best combination of coins that matches average transactions the most.
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 09:06:10 pm
nitacku you need to reduce the amount entered and left with put together
Title: Re: Least amount of change
Post by: nitacku on June 26, 2012, 09:13:20 pm
nitacku you need to reduce the amount entered and left with put together

I think the solution in that case is to bring nothing.

The reason: There is never 100% probability that you eliminate what you brought. Even if it was 100% probable that you would use what you brought, it would simply cancel itself. For example: If you bring one penny, 80% of the time it is used. This means that your average number of coins is 1 + (4.7 - 0.8). You see? 1 - 0.8 > 0, which means bringing anything defeats the purpose.
Title: Re: Least amount of change
Post by: ruler501 on June 26, 2012, 10:12:01 pm
I brute forced it too to see if that might show something other then the bring nothing

heres the code it runs with the standard C lib
Code: [Select]
#include <stdio.h>

typedef struct ChangeStruct
{
int pennies;
int nickels;
int dimes;
int quarters;
int amount;
} Change;

Change calculateChange(int amount)
{
int tempamount = amount;
Change result = {0,0,0,0,amount};
if (tempamount >= 25)
{
result.quarters = amount / 25;
tempamount -= result.quarters * 25;
}
if (tempamount >= 10)
{
result.dimes = tempamount / 10;
tempamount -= result.dimes * 10;
}
if (tempamount >= 5)
{
result.nickels = 1;
tempamount -= 5;
}
if (tempamount > 0)
{
result.pennies = tempamount;
}
return result;
}

int countCoins(Change counted)
{
int total;
total = counted.pennies + counted.nickels + counted.dimes + counted.quarters;
return total;
}

Change subtractChange(Change first, int second)
{
if (first.amount > second) return calculateChange(first.amount - second);
else return calculateChange(second - first.amount);
}

void printCoins(Change printNeeded)
{
printf("%d pennies\n%d nickels\n%d dimes\n%d quarters\n", printNeeded.pennies, printNeeded.nickels, printNeeded.dimes, printNeeded.quarters);
}

int main()
{
int smallestTotal, smallestE, currentE, smallestL, currentL, total;
Change smallestCoins, currentCoins, subtractCoins, returnCoins;
double smallestAverage=100.0, currentAverage;
for ( int i=0; i<100; i++)
{
total = 0;
currentCoins = calculateChange(i);
currentE = countCoins(currentCoins);
for ( int j=0; j<100; j++)
{
currentL = countCoins(subtractChange(currentCoins, j));
total += currentL + currentE;
}
currentAverage = total / 100.0;
if (currentAverage < smallestAverage)
{
smallestAverage = currentAverage;
smallestTotal = currentL + currentE;
smallestE = currentE;
smallestCoins = currentCoins;
}
}
printCoins(smallestCoins);
printf("With average %f\n", smallestAverage);
printf("Entered with %d coins\n", smallestE);
}

and the result was
0 pennies
0 nickels
0 dimes
0 quarters
With average 4.700000
Entered with 0 coins

This seems wrong to me so please help me find if there is an error in my code

EDIT: I fixed it so that 0 coins was an option
Title: Re: Least amount of change
Post by: thepenguin77 on June 27, 2012, 07:30:22 pm
Well, it looks like runer had it right from the start. Carrying 0 coins really is the best option.

I believe the solution is to carry no change.

Whatever the cost of your purchase is and whatever coins you have, you can ensure you leave with the minimum amount of change possible by giving the vendor all of your change. Whatever coins properly contribute to the payment, the vendor will keep; the coins that do not will be returned to you. Since the cents amount of the purchase is an equally-distributed random number, subtracting your constant amount of change from this will result in another equally-distributed random number (mod 100). Whatever amount of change you contribute, the vendor will always have to return to you coins that sum to an equally-distributed random amount of cents from 0-99.

You cannot limit the amount of coins you end with, so the optimal solution is simply to limit the amount of coins you begin with.

And with 2 brute force checks to back this, I'm happy. It looks like I've been doing it right all along. (Nitaku said his results were wrong and I think "someone" solved the wrong problem.