Tool to calculate the optimal change (dynamic programming) in order to minimize the number of coins and understand the algorithmic principles.
Change Making - dCode
Tag(s) : Arithmetics
dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!
The change problem is a fundamental problem in algorithms and combinatorial optimization. It consists of determining, for a given sum and a set of coin or banknote values, a combination that represents that exact sum using the minimum number of units.
To give change for 67 cents using 1, 2, 5, 10, 20, and 50 cent coins, an optimal solution is 50 + 10 + 5 + 2, using 4 coins.
A greedy algorithm for giving change consists of choosing, at each step, the highest possible coin or bill value that does not exceed the remaining amount.
Example: To give 67 cents in change using 1, 5, 10, 25, and 50 cent coins:
Take 50 cents (remainder: 17)
Take 10 cents (remainder: 7)
Take 5 cents (remainder: 2)
Take two 1-cent coins.
This strategy is simple and efficient, but it does not always guarantee an optimal solution.
A money system is said to be canonical if the greedy algorithm always provides a solution using the minimum number of coins.
Example: In the non-canonical system {1, 3, 4}, to give 6 cents in change, the greedy algorithm gives 4 + 1 + 1 (3 coins), while the optimal solution is 3 + 3 (2 coins).
When the system is not canonical, the greedy algorithm is not suitable and another approach is needed.
Dynamic programming allows us to solve the change-giving problem optimally for any coin system, by exploiting the fact that an optimal solution for a given sum is built from optimal solutions for smaller sums.
The key steps are as follows:
1 - Initialization: Create an array dp where dp[i] represents the minimum number of coins needed to give change for sum i. Initialize dp[0] = 0 and dp[i] to infinity for i > 0.
2 - Filling: For each sum i from 1 to the target_sum, and for each piece c, apply step 3.
3 - If c <= i, then dp[i] = min(dp[i], dp[i - c] + 1)
4 - The value dp[target_sum] gives the minimum number of pieces needed.
This approach guarantees optimality at the cost of higher computational efficiency.
dCode retains ownership of the "Change Making" source code. Any algorithm for the "Change Making" algorithm, applet or snippet or script (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or any "Change Making" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) or any database download or API access for "Change Making" or any other element are not public (except explicit open source licence). Same with the download for offline use on PC, mobile, tablet, iPhone or Android app.
Reminder: dCode is an educational and teaching resource, accessible online for free and for everyone.
The content of the page "Change Making" and its results may be freely copied and reused, including for commercial purposes, provided that dCode.fr is cited as the source (Creative Commons CC-BY free distribution license).
Exporting the results is free and can be done simply by clicking on the export icons ⤓ (.csv or .txt format) or ⧉ (copy and paste).
To cite dCode.fr on another website, use the link:
In a scientific article or book, the recommended bibliographic citation is: Change Making on dCode.fr [online website], retrieved on 2026-01-23,