I would like to code up a simple method of minimizing the number of coins required to give change. Then I would like see what coins are most likely to be called into usage if change is required from a uniform draw between 1 cent and 499 cents.
# I use the 200 and 100 for Canadian coins the Loonies and Twoonies. # The changer function will do what we want. # It is based on the concept that if we exhaust our biggest # currencies first then we will minize coin requirements. # This might not be the case if we had unusual coinage, # for instance a 30 cent peice would give 2x30 + 1xto + 1x5 = 75 cents # while 3x25 would be the preferred route. # However, since coinage tends to be divisible, in any example I can # think of using largest coins first always minimizes coin requirements. changer <- function(value, den) { # value is the change that must be made # den is the denominations available remainder <- value # Remainder counts how much change is still required to be made for count <- den # counts the required instances of each denomination names(count) <- den # loops through each of the denominations and removes the required # coinage for (i in 1:length(den)) { count[i] <- floor(remainder/den[i]) remainder <- remainder-count[i]*den[i] } count } # Define denominations to search through den <- c(200,100,25,10,5,1) changer(341,den) # 200 100 25 10 5 1 # 1 1 1 1 1 1 # Now let's see how many coins are required to make change for # all possible change between 1 cent and $4.99 cents. curcount <- NULL for (i in 1:499) curcount <- rbind(curcount, changer(i,den)) (meancur <- apply(curcount, 2, mean)) (meancur <- apply(curcount, 2, sum)) # 200 100 25 10 5 1 # 0.8016032 0.4008016 1.5030060 0.8016032 0.4008016 2.0040080 # We can see that the average transaction required .8 2 dollar coins, # .4 dollars, 1.5 quarters, .8 dimes, .4 nickels, and 2 pennies (ratiocur <- meancur/sum(meancur)) # 200 100 25 10 5 1 # 0.13559322 0.06779661 0.25423729 0.13559322 0.06779661 0.33898305 # Pennies had the largest ratio of 34% required while 25% of quarters # were required. den <- c(25,10,5,1) curcount <- NULL for (i in 1:99) curcount <- rbind(curcount, changer(i,den)) (meancur <- apply(curcount, 2, sum)) (meancur <- apply(curcount, 2, mean)) # 25 10 5 1 # 1.5151515 0.8080808 0.4040404 2.0202020 require(plotrix) pie3D(meancur, explode=0.3,radius=2.9, labels=c("quarters", "dimes", "nickles", "pennies"), main="Currency Ratios Required for Change")
(ratiocur <- meancur/sum(meancur)) # 25 10 5 1 # 0.31914894 0.17021277 0.08510638 0.42553191 # Once again pennies have the highest requirement at 42% of transactions # while quarters are next with 32%. Nickles are the least required with # only 8.5% of the ratio of required coins. # We might also want to know for what percentage of transactions certain # coins are required. We can do this one mostly in our head. Pennies will # be required whenever change is not divisible by 5, thus ~4/5 times. # Quarters will be required whenever the change is greater than 24 cents, # thus ~75/99=757. Dimes will be required when whatever remains # after dividing by quarters is greater than 9 cents, thus ~15/25=60%. # Finally, nickles are required whenever whatever is left after quarters # and dimes is greater or equal to 5. 25 -> 1,2,3,4,x5,x6,x7,x8,x9,0,1,2, # 3,4,x5,x6,x7,x8,x9,0,1,2,3,4 so, ~10/25=40%. # Let's check. apply(curcount>0, 2, mean) # 25 10 5 1 # 0.7575758 0.6060606 0.4040404 0.8080808 # Thus we can see that though nickles represent a small portion of the # optimal ratio of currency in circulation, the do represent a large # portion of the optimal change patterns required.
