# Recursion in C++ : The Coin Changing Problem

March 9, 2011 4 Comments

Let’s say that we have an infinite supply of pennies ( 1 cent ), nickels ( 5 cents ), dimes (10 cents ) and quarters (25 cents). (That would probably be a nice thing — having an infinite number of coins, whatever be the denomination. On a different note, the fact that this question assumes such an infinite supply of coins should convince everyone of the hypothetical nature of this and similar problems. That said, it is sometimes useful to get a fair idea of how to go about solving seemingly contrived problems in order to tackle realistic ones.)

Coming back to the problem — We buy a commodity and are required to pay cents for it. Now, the interesting bit is that we are required to pay exactly cents, i.e. we cannot pay more and get change in return. So, the question is — How many ways can we possibly pay cents using our infinite supply of coins?

The question could be rephrased thus:

Given the equation: where are non-negative integers, find the total number of possible solutions to the equation.

——-

Let’s take an example to clarify things — say. n = 91.

Let denote the number of ways can be represented in terms of coins having denomination 1, 5, 10 and 25.

Thus, our task is to compute .

The coin of the highest denomination is that of 25 cents.

We have 2 choices before us: (1) use at least one 25-cent coin, or (2) do not use 25-cent coins at all.

is the sum of the number of ways we can perform (1) and (2).

(1) If we decide to use at least one 25-cent coin:

We can see that we can use either 1, 2 or 3 such coins.

If we use 1 25-cent coin, we are left with 66 cents to be represented using coins of denominations 1, 5 and 10. So, we now see that we have a smaller subproblem — that of representing 66 cents using 1, 5 and 10 cent coins.

We digress here to make a remark about notation.

—-

Let us denote by the total number of ways in which we can represent cents using coins of denomination with the added condition that at least 1 coin of denomination is used.

Thus, we have that .

(The 1st term denotes the number of ways we can represent cents using 1, 5, 10 and 25-cent coins such that at least 1 25-cent coin is used. The 2nd term — f(n,10) denotes the number of ways wherein we do not use any 25-cent coins and use at least 1 10-cent coin. Similarly, we can work out the meaning of the remaining terms. It can be observed that the events represented by each of the these terms are in some sense disjoint, which is why we can simply add these terms to get our desired result.)

——–

Going back to our example, the number of ways we can represent 66 cents using coins of denomination only 1, 5 or 10 cents is equal to .

—-

We again digress to make another remark on notation.

We use to denote the number of ways in which we can represent cents using coins of denominations .

Hence, our original task, which was to compute is the same as computing .

——–

Again, coming back to the example, we see that the number of ways to represent cents using 1, 5 and 10-cent coins is simply .

—

Now, what if we decide to use 2 25-cent coins in representing 91 cents. Then, we are left with representing 41 cents using 1, 5 and 10-cent coins, which can be done in ways.

Similarly, we can see that we can represent 91 cents using 3 25-cent coins in ways.

—-

A little reflection would get us the following equation:-

———

We can now write the method discussed above in the form of a function in C++.

——-

**#include <iostream.h> **

**// Let’s say we have a global array A[4] = {25, 10, 5, 1}. **

//**func F takes a non-negative input n, and an index into the array A. **

**// It returns f (n, A[i]) [where the notation f(n,k) is as discussed earlier.]
**

**int F (int n, int index) ; **

**// func G takes non-negative input n, and an index into the array A, and returns g(n,A[i]) as discussed above. **

**int G (int n, int index) // the initial call from the main ( ) function would be G(n,0).
**

**{**

**int i = index; **

**int ans = 0; **

**for ( ; i < 4; i++) **

**ans + = F (n, i); **

**return ans;
**

**}**

**//Now we give the implementation of func F. **

**int F ( int n, int index ) **

**{**

**if ( n < A[index] ) **

**return 0; **

**if ( index == 3) **

**return 1; // because f(n,1) == 1.
**

**int ans = 0, quotient = n/ A[index]; **

**for (int i = 1; i <= quotient; i++) **

**{**

**ans + = G (n – i*A[index], index+1)
**

**}**

**return ans;
**

**}**

**———
**

awesome article!!

thnx a lot for this…..this problem looks easy on the surface but it is very complex(thnx to recursion)….

but u did a perfect job of explaining

Thank you so much for your comment. I’m glad that you liked it.

nice explanation🙂

Thanks.