Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created November 18, 2013 05:18
Show Gist options
  • Save animatedlew/7522911 to your computer and use it in GitHub Desktop.
Save animatedlew/7522911 to your computer and use it in GitHub Desktop.
Finding how many combinations of change can be produced based on a total amount and list of denominations. Note that the Underscore.js dependency is required for legibility.
(function(_) {
var money = 300;
var coins = [5, 10, 20, 50, 100, 200, 500];
var countChange = function(money, coins) {
if (money == 0) return 1;
else if (_.isEmpty(coins) || money < 0) return 0;
else return countChange(money - _.head(coins), coins) + countChange(money, _.tail(coins));
};
// 1022
console.log(countChange(money, coins));
}).call(this, _);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment