I'm a lurking layman trying to teach myself CS and am working my way through the first chapter of SICP at the moment.
I've come across the "counting change" example procedure and, like some others, although I understand the procedure itself, I'm struggling with the premise on which it is based:
Suppose we think of the types of coins available as arranged in some order. Then the following relation holds:
The number of ways to change amount a using n kinds of coins equals
the number of ways to change amount a using all but the first kind of coin, plus
the number of ways to change amount a - d using all n kinds of coins, where d is the denomination of the first kind of coin.
To see why this is true, observe that the ways to make change can be divided into two groups: those that do not use any of the first kind of coin, and those that do. Therefore, the total number of ways to make change for some amount is equal to the number of ways to make change for the amount without using any of the first kind of coin, plus the number of ways to make change assuming that we do use the first kind of coin. But the latter number is equal to the number of ways to make change for the amount that remains after using a coin of the first kind.
The problem and procedure are available in context here, but I won't include the procedure as it's the logic above which I'm struggling with and not the procedure itself.
What I fail to understand is why these are always equal:
- The total number of "ways", less the "ways" to make the full amount without using the first type of coin.
- The number of "ways" using all types of coins to make the remainder of the amount, after subtracting the denomination of the first coin.
I've run through countless test examples manually, and yes, the relationship holds true, but I don't understand why.
The first sentence of their explanation makes sense:
observe that the ways to make change can be divided into two groups: those that do not use any of the first kind of coin, and those that do.
If we have the total number of "ways", we can divide that total into two groups according to that rule. No problem.
The second sentence is also clear:
Therefore, the total number of ways to make change for some amount is equal to the number of ways to make change for the amount without using any of the first kind of coin, plus the number of ways to make change assuming that we do use the first kind of coin.
This simply states that the total number of ways is the sum of the ways that have been divided into two categories. OK.
This is where they lose me:
the latter number is equal to the number of ways to make change for the amount that remains after using a coin of the first kind.
I assume this means that the "latter number" refers to all the ways to make change that must use at least one coin from the first denomination.
Why and how does this number equal the number of ways to make change with all coins for the total amount less the denomination of the first coin?
To try and visualise the problem, I restated it as trying to achieve a particular weight of 80Kg on a barbell with a set of 3 weight denominations:
- Red: 5Kg
- Green: 10Kg
- Blue: 20Kg
I layed each scenario out visually and indeed the relationship holds, but I still don't understand why:
It feels as though the answer to this is right under my nose, but perhaps the sheer size of that particular appendage will eternally obscure the answer.
ways(denomsList, targetSum)
, using zero or more coins from each denomination. either you use the first denomination coin,ways( denomsList, targetSum - first(denomsList))
; or you never do:ways( rest(denomsList), targetSum)
. and of course, ifA == append(B,C)
thenlength(A) == length(B) + length(C)
. – Will Nessways(denomsList, targetSum)
, then clearly the number of piles in the second group isways( rest(denomsList), targetSum)
, (contd.) – Will NesstargetSum - first(denomsList)
hence their number isways(denomsList, targetSum - first(denomsList))
in total. and you're welcome. :) – Will Ness