Created
January 14, 2015 17:04
-
-
Save tonyedgecombe/e293d4d6f14d91139a0a to your computer and use it in GitHub Desktop.
Ways to make a pound
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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