Skip to content

Instantly share code, notes, and snippets.

@tomca32
Created July 30, 2013 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomca32/6115134 to your computer and use it in GitHub Desktop.
Save tomca32/6115134 to your computer and use it in GitHub Desktop.
Coin sums problem recursively in JS
;
//Project Euler Problem #4 - Largest Palindrome Product
(function (exports) {
"use strict";
var coins = [1,2,5,10,20,50,100,200];
exports.change = function (money,i) {
if (money === 1 || money ===0) return 1;
if (i <0) return 0;
if (typeof i ==='undefined') i = coins.length-1;
if (coins[i] > money) return exports.change(money, i-1);
// console.log(money, i);
return exports.change(money-coins[i], i) + exports.change(money, i-1);
};
}(global));
console.log(change(200));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment