Skip to content

Instantly share code, notes, and snippets.

@sagartalla
Last active July 11, 2019 09:59
Show Gist options
  • Save sagartalla/618255674c3f228e53082af5c30d06a6 to your computer and use it in GitHub Desktop.
Save sagartalla/618255674c3f228e53082af5c30d06a6 to your computer and use it in GitHub Desktop.
Coins: Given an infinite number of quarters (25 cents), dimes (1 O cents), nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of representing n cents.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/*
Coins: Given an infinite number of quarters (25 cents), dimes (1 O cents), nickels (5 cents), and
pennies (1 cent), write code to calculate the number of ways of representing n cents.
*/
var L = console.log;
var m = [];
function denominations(a, d, i) {
if(m[a] && m[a][i]) {
return m[a][i];
}
if(i > d.length - 1) return 1;
console.log(a + ' ' + d[i] + ' ' + m);
var w = 0;
for(var k = 0; k * d[i] < a; k++) {
w += denominations(a - k * d[i], d, i+1);
}
m = m || [];
m[a] = m[a] || [];
m[a][i] = w;
return w;
}
L(denominations(100, [25, 10, 5, 1], 0))
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
Coins: Given an infinite number of quarters (25 cents), dimes (1 O cents), nickels (5 cents), and
pennies (1 cent), write code to calculate the number of ways of representing n cents.
*/
var L = console.log;
var m = [];
function denominations(a, d, i) {
if(m[a] && m[a][i]) {
return m[a][i];
}
if(i > d.length - 1) return 1;
console.log(a + ' ' + d[i] + ' ' + m);
var w = 0;
for(var k = 0; k * d[i] < a; k++) {
w += denominations(a - k * d[i], d, i+1);
}
m = m || [];
m[a] = m[a] || [];
m[a][i] = w;
return w;
}
L(denominations(100, [25, 10, 5, 1], 0))</script></body>
</html>
/*
Coins: Given an infinite number of quarters (25 cents), dimes (1 O cents), nickels (5 cents), and
pennies (1 cent), write code to calculate the number of ways of representing n cents.
*/
var L = console.log;
var m = [];
function denominations(a, d, i) {
if(m[a] && m[a][i]) {
return m[a][i];
}
if(i > d.length - 1) return 1;
console.log(a + ' ' + d[i] + ' ' + m);
var w = 0;
for(var k = 0; k * d[i] < a; k++) {
w += denominations(a - k * d[i], d, i+1);
}
m = m || [];
m[a] = m[a] || [];
m[a][i] = w;
return w;
}
L(denominations(100, [25, 10, 5, 1], 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment