Skip to content

Instantly share code, notes, and snippets.

@huttj
Created May 27, 2016 22:28
Show Gist options
  • Save huttj/5f2c18e62f3a01d29ce4d1786ed50be6 to your computer and use it in GitHub Desktop.
Save huttj/5f2c18e62f3a01d29ce4d1786ed50be6 to your computer and use it in GitHub Desktop.
'use strict';
const coins = [100,25,10,5,1];
const optimal = coinCount(48);
console.log(coinCount(25, without(coins, 25)));
console.log(
optimal.map(n => coinCount(n)).join('\n')
);
function coinCount(number, coins) {
coins = coins || [100,25,10,5,1];
const used = [];
let head;
while (number > 0) {
head = coins[0];
if (number >= head) {
number -= head;
used.push(head);
} else {
coins.shift();
}
}
return used;
}
function sum(arry) {
return arry.reduce((a,b) => a + b, 0);
}
function factors(n, less) {
const all = [];
const f = coinCount(n, without(coins, n));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment