# Case Study Designing A Coin Changer

By In 1

More formally, input to the problem is integer money and positive integers,

coin1, coin2, coind, that represents coin denominations.

For example in the US, coin1 will be 1 cents,

coin2 will be 5 cents, 10 cents, 25 cents, and 50 cents.

And the output is the minimum number of coins, with denominations coin1,

coin2, coind that changes money exactly.

So today in the morning, when cashier had to return me 40 cents,

she most likely used the following algorithm.

First, finding the largest coin denomination

that is smaller than 40 cents.

It will be 25 cents.

So she gave me 25, 15 cents left and

then the next challenge is how to change 15 cents.

It is the minimal number of coins in the United States.

But if you travel to Tanzania, it won't be

the minimum number of coins because there is a 20 cent coin in Tanzania.

And therefore this greedy approach to solving the change

problem will fail in Tanzania because there is a better way to change 40 cents,

simply as 20 cents plus 20 cents, using Tanzanian 20 cents coin.

Since the greedy approach to solving the change problem failed,

let's try something different.

Let's try the recursive algorithm for solving the same problem.

Suppose we want to change 9 cents, and

our denominations are 1 cent, 5 cents, and 6 cents.

What would be the optimal way to change 9 cents?

Well, if we only knew what is the optimal ways to change 9 minus 6 cents,

9 minus 5 cents and 9 minus 1 cents, then we would know,

what is the optimal way to change 9 cents?

In other words, to change 9 cents,

we need to know how to change small number of cents,

in our case, 3 cents, 4 cents, and 8 cents.

And therefore, an approach to solving this problem would be

to use this recurrence to write the recursive program.

To change money, cents using coins, coin1,

coin2, coind, we do the following.

We first recursively call RecursiveChange with the amount of money,

money minus coin1, money minus coin2, and money minus coind.

And find the minimum amount of money for these d choices.

We have plus 1 because there is one more coin to add and returns this way.

It turn out that we will need to call it trillions of times,

which means that our seemingly very elegant

So as simple as the change problem looks like, neither a greedy approach nor

a recursive approach solve it in a reasonable time.

60 years ago, a brilliant mathematician, Richard Bellman, had a different idea.

And the key idea of dynamic programming is to start filling this matrix,

not from the right to the left, as we did before in the recursive change, but

instead, from the left to the right.

So, we will first ask the trivial question,

what is the minimum number of coins needed to change 0 cents?

And, of course, it is 0.

What is the minimum number of coins to change 1 cents?

Obviously it is one, but we can compute this number by finding what

is the minimum number of coins to change 0 cents and adding one coin.

We will proceed in a similar fashion to compute the minimum number of

coins to change 2 cents, 3 cents, and 4 cents.

There is only one possibility to derive this number from the previous number.

And for 5 cents, actually there are two possibilities, green and blue.

For green one, you can derive it from 0 cents by adding 5 coins,

and for blue possibility, we can derive it from 4 cents by adding one penny.

Well, which possibility would you select?

Of course the one that gives you the minimum change for 5 coins.

And continue further and apply the code to 6 cents and there are three possibilities and once

again we select the optimal choice that correspond to minimum number of coins.

Let's say it may be 0 coins plus 6 cents.

We continue for 7 cents, continue for 8 cents, and finally,

very quickly, we actually found the correct answer for 9 cents.

We need four coins to change 9 cents.

And this results in DPChange algorithm that simply fills

up the table that I just showed you from left to right.

And he wanted to hide that he's really doing mathematics from the Secretary

of Defense, rather than working on Air Force project.

Therefore he invented a name

that basically has nothing to do with what dynamic programming algorithms do.

In his own word, he said, what name could I choose?

I was interested in planning but planning is not a good word for various reasons.

I decided therefore to use the word programming, and

I wanted to get across the idea that this was dynamic.

It was something not even a Congressman could object.

If your business accepts coins as payment, you know that manually counting them at the end of the day isn’t very productive. It’s boring, it’s time consuming, and it’s a waste of time. Businesses like laundromats, car washes, amusement parks, banks, and retail stores can greatly benefit from investing in coin counters.

A coin counter can speed up the process and make your life easier.

If you’re still manually counting coins, it’s time for a change. It’s time to take advantage of technology. Here are the top five benefits you’ll get to look forward to when you invest in a coin counter.

## Save Valuable Time

These machines count coins of the same denomination quickly and effortlessly. Simply separate your coins and dump them into the machine for easy counting. They are incredibly fast—faster than any employee you have on staff—so you will save valuable time. Once the target amount has been reached on the counter, the machine will stop, provide a total, and alert you to add more coins in. All you have to do is move the counted bag of coins out of the way and dump more in so it can resume counting for you.

When your employees aren’t spending so much time on counting coins in the back room, they can spend more time with your customers and on more valuable responsibilities, like cleaning up.

## Greater Accuracy

Manual coin counting is difficult and cumbersome to do accurately. It inevitably comes with errors—it’s easy to lose track and make counting and reporting mistakes when you’re counting large volumes of coins. Your employees are human, after all. But just one mistake made while counting coins can add up to an extra 20 minutes or more of recounting—which is a waste of time. When you invest in a counter, you get greater accuracy, so you know that your totals will be reported correctly every time.

## Decrease in Costs and Losses

Regardless of how common they are, when coin counting mistakes occur, they cost you money. Miscounting a few nickels might not seem significant, but miscounting dollar and two dollar coins can quickly add up. Luckily, your new coin counter will guarantee 99.9 percent accuracy, so you won’t have to worry about losing out on your hard-earned cash because of human error.

And here’s an added bonus: because a coin counter can save you so much time, you can decrease your labour costs. You can cut your employees shifts by sending them home earlier since they won’t have to stay late to count coins anymore.

Internal theft can account for a significant portion of your company’s overall losses. As much as you wish they were, not all employees are honest. Investing in a coin counter should be a part of your loss prevention strategy.

When dishonest workers are counting coins by hand, it can be pretty easy for them to slip some of them in their pockets when no one is looking. And although they might just be small coins, they’ll add up over time. There’s no reason for you to be losing out because of internal theft. Let the machine take care of the counting and keep your money out of your employees’ hands as much as possible.

## Clean Hands

Coins are dirty. They’ve been passed around from hand to hand and touched a lot of filthy surfaces before coming into your possession. Keep your employees’ hands clean and germ free by letting the machine do the grunt work.

## A Change for the Better

Whether you’re counting coins in bags or rolling them up for depositing, your organization can benefit from investing in a coin counter. There’s no reason to be counting them manually. Make a change for the better and you’ll enjoy time and cost savings, greater accuracy, reduced opportunities for internal theft, and cleaner hands.