Coin Machine

This Demonstration shows a coin machine. Coins are available in denominations of 1, 5, 10, 25, and 50 cents and 1 dollar. Specific coins can be excluded and you can refill the machine. The coin machine can provide change for a given amount of money. Three algorithms are presented: the greedy algorithm, the optimal algorithm for the given system, and a random algorithm.


  • [Snapshot]
  • [Snapshot]
  • [Snapshot]


A sequence where is called a currency or a coinage system. We always assume that is the smallest coin, so that every cash amount can be represented in the system in at least one way. In our case, any coinage system can be chosen using the coins available in the current US system, but the 1 cent coin must be included.
The change-making problem asks for the most efficient algorithm for the given coinage system (efficient in the sense of using the least number of coins). For most coinage systems, the greedy algorithm (which at each step chooses the maximum possible number of the largest denominations available) is optimal, but this is not true for general systems. Famously, this was not the case for the pre-decimal British coinage system.
The greedy algorithm is optimal for most subsets of ; however, it is not optimal for subsets , , , and . The first amount of money for which these coinage systems are not optimal is 30 cents. This can be verified using the Pearson test (see [1, 2]), which is implemented in this Demonstration. The smallest amount of money for which the optimal algorithm does not give the same answer as the greedy algorithm is 30 cents.
[1] M. Kleber, R. Vakil, and J. Shallit, "What This Country Needs Is an 18c Piece," Mathematical Intelligencer 25(2), 2003 pp. 20–23. doi: 10.1007/BF02984830.
[2] M. Adamaszek and A. Niewiarowska, "Combinatorics of the Change-Making Problem," European Journal of Combinatorics 31(1), 2010 pp. 47–63. arXiv:0801.0120v2.
    • Share:

Embed Interactive Demonstration New!

Just copy and paste this snippet of JavaScript code into your website or blog to put the live Demonstration on your site. More details »

Files require Wolfram CDF Player or Mathematica.