Coin change tree
About this app
How many ways are there to change 5 cents with 1 and 2 cent coins? The recursion tree W(a,n) = W(a,n−1) + W(a−d,n) expands node by node or runs in Python’s call order: left without the coin type, right one coin taken. Every leaf W(0,·) is one way, and the path to it shows the coins; 0-leaves carry their reason. Counters for calls and distinct subproblems, equal subproblems in colour, a memory version as an outlook, a table up to 300 cents.
With explain mode: an AI tutor walks you through the app, points out what matters in the scene and answers your questions.
The recursion W(a,n)
W(a,n) counts the ways to change a cents with the first n coin types. Every way either does not use coin type n at all – the amount stays and one coin type drops out, W(a,n−1) – or uses it at least once: one coin comes off and all coin types stay, W(a−d,n). The recursion stops at amount 0 with one way found, and at a negative amount or with no coin type left with zero. Everything is in cents: 2 euros are W(200,6).
What the app shows
The recursion tree grows node by node: every pill W(a,n) has on the left the child without the largest coin type and on the right the child after taking one coin, which sits on the edge as a coin. Leaves with 1 are ways, leaves with 0 carry their reason. Finished nodes write the sum of their children below them, such as 1 + 2 = 3. Next to the tree are the coin stack on the path, the ways found and counters for calls and distinct subproblems.
Try it yourself
Start with W(5,2), make an estimate and expand the tree by clicking, or let Step run it in the order in which Python makes the calls. Clicking a leaf shows its path and its coins. Further examples are W(6,3), all six coin types and W(10,3), plus your own amounts up to 12 cents. The equal subproblems switch colours repetitions, the memory version saves them, and the Python program can be copied.
Frequently asked questions
- How many ways are there to change 5 cents with 1 and 2 cent coins?
- Three: 1+1+1+1+1, 2+1+1+1 and 2+2+1. The recursion tree W(5,2) finds them as the three leaves with amount 0. It needs 25 calls for that, because the other ten leaves are dead ends with value 0.
- Why does the recursion not count 2+1+1+1 and 1+2+1+1 twice?
- A path first takes coins of the largest remaining type and then removes that type for the rest. So the larger coins always come first on the path, and every combination arises exactly once.
- How many ways are there to change 2 euros with all six coin types?
- W(200,6) gives 69118 ways with the coins 1, 2, 5, 10, 20 and 50 cents. The plain recursion needs 5567019 calls for that; with a memory for subproblems computed before it needs 961.
- What can I set in the Coin Change Tree app?
- Amounts from 1 to 12 cents and 1 to 6 coin types, or four ready-made examples. The tree can be expanded freely or played step by step in Python order; there are also equal subproblems in colour, a memory version and a table up to 300 cents.
Subject: Computer science
More from Computer science
- Ackermann function
- Call Tree
- Elevator simulator
- Binary workshop
- Binary versus linear search
- The Collatz problem
- Inside a CPU
- Swap the inside
A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps