Coins
8.11 Coins: Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents), and pennies (1 cent), write code to calculate the number of ways of representing n cents.
I first decided to take the brute force approach. It runs fine, but the solution isnt correct. I still include it because I think the idea is valuable.
What I did is take the power set of the permutations to find all the possible subcombinations, and their permutations. After generating these, (which there are a reletively few number of, as QDNP is only 4 characters, and the power set grows at 2^k, which perms at k!) this only generates 65 different permutations of subcombinations.
I then ran these through a function that would mod all the elements in their strict order in order to find the ones that work (generate change == 0).
The main problem with this is that now each 'command' has to be iterated through a several number of different times. E.g. "DQ" does not only mean maxing out Dimes and then Quarters, as my algorithm does. It means finding and all combinations of n-number of dimes, and n-number of quarters.
// wrong!
New approach:
Given just 3 different coins, we can represent this problem visually:
Some combination of 25, 10, and 5 to represent a 3-dimensional plane.
If each distinct integer of the plane
However when we limit the graph to its realistic dimensions
ContourPlot3D[25 x+10 y+5 z==100, {x, 0, 22}, {y, 0, 22}, {z, 0, 22}]
, we get something like this:
Which contains a limited surface area we can calculate. Each distinct whole number point represents a solution. So the surface area represents the total sum of solutions.
The surface area is a double integral...
Can apply the same idea with in a 4 dimensional solution, like the problem asks.
Last updated