Skip to content

Instantly share code, notes, and snippets.

@russelldb
Created October 10, 2009 22:19
Show Gist options
  • Select an option

  • Save russelldb/207194 to your computer and use it in GitHub Desktop.

Select an option

Save russelldb/207194 to your computer and use it in GitHub Desktop.
-module(change).
-compile(export_all).
count_change(Amount, CoinSet) ->
FList = lists:sort(CoinSet),
Fd = fun(X) ->
lists:nth(X, FList) end,
cc(Amount, length(CoinSet), Fd).
cc(0, _C, _Fd) -> 1;
cc(N, C, _Fd) when N < 0 ; C =:= 0-> 0;
cc(Amount, Coins, Fd) ->
cc(Amount, Coins-1, Fd) + cc(Amount - Fd(Coins), Coins, Fd).
%% Thanks http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment