Skip to content

Instantly share code, notes, and snippets.

@tonyedgecombe
Created January 14, 2015 17:04
Show Gist options
  • Save tonyedgecombe/e293d4d6f14d91139a0a to your computer and use it in GitHub Desktop.
Save tonyedgecombe/e293d4d6f14d91139a0a to your computer and use it in GitHub Desktop.
Ways to make a pound
// Calculate the ways to make a pound from UK change
// UK common coinage, must be ordered high to low
var coinage = [100, 50, 20, 10, 5, 2, 1];
// £1.00
var total = 100;
var result = count(coinage, total);
console.log(total + " can be made up in " + result + " ways"); // 4563
function count(coins, amount) {
if (amount === 0) {
return 1;
}
else if (amount < 0 || coins.length === 0) {
return 0;
}
else {
var first = coins[0];
var rest = coins.slice(1);
return count(rest, amount) + count(coins, amount - first);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment