{"id":44073,"date":"2014-09-15T07:00:00","date_gmt":"2014-09-15T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/09\/15\/enumerating-all-the-ways-of-making-change\/"},"modified":"2014-09-15T07:00:00","modified_gmt":"2014-09-15T07:00:00","slug":"enumerating-all-the-ways-of-making-change","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140915-00\/?p=44073","title":{"rendered":"Enumerating all the ways of making change"},"content":{"rendered":"<p>\nToday&#8217;s Little Program enumerates all the ways of making\nchange for a particular amount given a set of available denominations.\n<\/p>\n<p>\nThe algorithm is straightforward.\nTo make change for a specific amount from a set of available\ndenominations,\nyou can take one denomination and decide\nhow many of those you want to use.\nThen use the remaining denominations to make change for the remainder.\n<\/p>\n<p>\nFor example, if the available coins have values [1, 5, 10, 25]\nand you want to make change for 60 cents, you first decide how many\n25-cent pieces you want to use.\nIf you use none, then you need to make change for 60 cents\nusing just [1, 5, 10].\nIf you use one, then you need to make change for 35 cents\nusing just [1, 5, 10].\nAnd if you use two, then you need to make change for 10 cents\nusing just [1, 5, 10].\n<\/p>\n<p>\n(We use the largest coin first to reduce the number of dead ends,\nlike asking &#8220;Please make change for 83 cents using only 10-cent coins.&#8221;)\n<\/p>\n<pre>\nfunction MakeChange(coins, total, f) {\n if (total == 0) { f([]); return; }\n if (coins.length == 0) return;\n var coin = coins[coins.length - 1];\n var remaining = coins.slice(0, -1);\n var used = [];\n for (var target = total; target &gt;= 0; target -= coin) {\n  MakeChange(remaining, target, function(s) {\n   f(used.concat(s));\n  });\n  used.push(coin);\n }\n}\nMakeChange([1, 5, 10, 25], 60, console.log.bind(console));\n<\/pre>\n<p>\nFirst, we take care of some base cases.\nTo make change for zero cents, we simply use zero coins.\nAnd it&#8217;s not possible to make\nchange for a nonzero amount with no coins.\n<\/p>\n<p>\nOtherwise, we take the highest denomination coin\nand try using zero of them, then one of them, and so on,\nuntil we exceed the total amount necessary.\n<\/p>\n<p>\nThere are related problems, such as determining whether\na particular amount of change can even be made, given a\ncollection of denominations\nand calculating\n<a HREF=\"http:\/\/www.algorithmist.com\/index.php\/Coin_Change\">\nthe number of ways change can be made<\/a>\nrather than enumerating them.\nBut I like enumeration problems.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program enumerates all the ways of making change for a particular amount given a set of available denominations. The algorithm is straightforward. To make change for a specific amount from a set of available denominations, you can take one denomination and decide how many of those you want to use. Then use the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-44073","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program enumerates all the ways of making change for a particular amount given a set of available denominations. The algorithm is straightforward. To make change for a specific amount from a set of available denominations, you can take one denomination and decide how many of those you want to use. Then use the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=44073"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44073\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=44073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}