DQ DQbJA%ozyqozozyqoz{w?cozoziii{mx=K{mK{W@ihp{mhozyqozvhh}h?c=ozoz@ipiy]hhx''aAaEABDBXB-~| G776669FZBCCZXB~~Z~CWYZ-

Change, coins.` Change is made with a recursive method. This sort of problem, as described in the Structure and Interpretation of Computer Programs, can be solved with recursion.`This implementation` in the C# language is illustrative. For most of these puzzles, implementing the code yourself (with a little help) is a good way to learn. `Recursion `recursion`Example.` This program uses recursion to compute different ways of making change to match a specified amount. Change() will print out all the ways you can make change for a specified amount. `Example: `If you specify 51 cents, it will tell you can make this out of 36 1-cent coins and three 5-cent coins.`Notes, above program.` The 2 List instances declared store the currently recorded values (which starts empty), and the amounts or denominations of coins that are possible to be used. `List `list`Int `int`The program emphasizes the recursive Change method. This implements the logic that actually computes the change.`In many recursive methods, it is clearest to add the exit conditions at the start of the function.`Notes, Change method.` Change() first tests for the ideal amount, and if this is reached, it prints it to the screen. It tests to make sure the change collected has not exceeded the target. `If `if`Notes, size.` This algorithm only adds coins in increasing size. It starts with the 1-cent coin and continues with the 5-cent coin and then larger coins. The output is automatically sorted.`Output.` Display() prints the frequencies of the coins that can be added together to reach the target value. In the example, all of the output will add up to 51 cents. `Console.WriteLine `console`The first output: `It shows 51 cents being composed of 51 1-cent coins. This is obviously a correct answer.`The second output: `It shows 51 cents being made up of 46 1-cent coins and one 5-cent coin.`SICP.` The Structure and Interpretation of Computer Programs book uses this example in its first chapter. It counts ways to compute change for 100 cents. See section 1.2.2 Tree Recursion. `SICP text: mit.edu `https://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2`Answer: `The answer of 292 is computed from interpreting the Lisp program. On the C# program shown here, you will also get 292 answers.`If you modify the program to include a counter, it is clearer that the result is 292.`Performance.` This program is far from ideal for performance. A variety of approaches could be used to transform the process into a faster one. `You could use arrays instead of List types, and could use a Stack structure instead of a recursive method.`Arrays `array`Stack `stack`Performance, continued.` You could improve the loop with a faster algorithm that jumps to the correct starting index based on the current top value. This could be done with a lookup table. `Foreach `foreach`A summary.` We wrote an example program that makes change to match a specified amount. It uses a recursive method invocation and the List type to solve a puzzle.

EFH FF; FFF.LinqFFF { EFE{ EEF<F> coinsFFF<F>(); EEF<F> amountsFFF<F>() { 1, 5, 10, 25, 50 };H EE// EEFCompute change F51 cents. EE// EEHChangeH(coins, amounts, 0, 0, 51); E} EFFHChangeH(F<F> coins, F<F> amounts, Fhighest, Fsum, Fgoal) E{H EE// EEFSee if we are done. EE// EEHFsumFgoal) EE{ EEEF(coins, amounts); EEEF; EE}H EE// EEFSee if we have too much. EE// EEHFsum > goal) EE{ EEEF; EE}H EE// EEFFthrough amounts. EE// EEHF (FFFamounts) EE{H EEE// EEEFOnly add higher or equal amounts. EEE// EEEHFF >= highest) EEE{ EEEEF<F> copyFFF<F>(coins); EEEEcopy.FF); EEEEHChangeH(copy, amounts, F, sumFF, goal); EEE} EE} E} EFFF(F<F> coins, F<F> amounts) E{ EEF (FamountFamounts) EE{ EEEFcountFcoins.F(F => FFamount); EEEF"{0}: {1}", EEEEamount, EEEEcount); EE} EEF); E} } HOutput: partialH 1: 51 5: 0 10: 0 25: 0 50: 0 1: 46 5: 1 10: 0 25: 0 50: 0 1: 41 5: 2 10: 0 25: 0 50: 0 1: 41 5: 0 10: 1 25: 0 50: 0 1: 36 5: 3 10: 0 25: 0 50: 0H

!-counts change