Skip to content

Instantly share code, notes, and snippets.

@laltin
Last active August 6, 2016 21:21
Show Gist options
  • Save laltin/0ac5ef9ad84c6c0d62da75d375f8ea28 to your computer and use it in GitHub Desktop.
Save laltin/0ac5ef9ad84c6c0d62da75d375f8ea28 to your computer and use it in GitHub Desktop.
Solution to Project Euler problem 31
<html>
<head>
<head>
<body>
<script>
var coins = [1, 2, 5, 10, 20, 50, 100, 200];
function ways(x, limit=0) {
if (x < 0) {
return 0;
}
if (x == 0) {
return 1;
}
var n = 0;
for (var i = 0; i < coins.length; i++) {
if (coins[i] <= limit)
continue;
for (var j = x - coins[i]; j >= 0; j -= coins[i]) {
n += ways(j, coins[i]);
};
}
return n;
}
document.writeln(ways(200));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment