How to resolve the algorithm Count the coins step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Count the coins step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

There are four types of common coins in   US   currency:

There are six ways to make change for 15 cents:

How many ways are there to make change for a dollar using these common coins?     (1 dollar = 100 cents).

Less common are dollar coins (100 cents);   and very rare are half dollars (50 cents).   With the addition of these two coins, how many ways are there to make change for $1000? (Note:   the answer is larger than   232).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Count the coins step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main()

   US_coins       := [1, 5, 10, 25]
   US_allcoins    := [1,5,10,25,50,100]
   EU_coins       := [1, 2, 5, 10, 20, 50, 100, 200]
   CDN_coins      := [1,5,10,25,100,200]
   CDN_allcoins   := [1,5,10,25,50,100,200]

   every trans := ![ [15,US_coins], 
                     [100,US_coins], 
                     [1000*100,US_allcoins] 
                  ] do 
      printf("There are %i ways to count change for %i using %s coins.\n",CountCoins!trans,trans[1],ShowList(trans[2]))
end

procedure ShowList(L)            # helper list to string 
every (s := "[ ") ||:= !L || " "
return s || "]"
end


procedure CountCoins(amt,coins)  # very slow, recurse by coin value
local count
static S

if type(coins) == "list" then {
   S := sort(set(coins))
   if *S < 1 then runerr(205,coins)
   return  CountCoins(amt)
   }
else {
   /coins := 1
   if value := S[coins] then {
      every (count := 0) +:= CountCoins(amt - (0 to amt by value), coins + 1) 
      return count
      }   
   else    
      return (amt ~= 0) | 1
   }
end


# coin.icn
# usage: coin value
procedure count(coinlist, value)
	if value = 0 then return 1
	if value < 0 then return 0
	if (*coinlist <= 0) & (value >= 1) then return 0
	return count(coinlist[1:*coinlist], value) + count(coinlist, value - coinlist[*coinlist])
end


procedure main(params)
	money := params[1]
	coins := [1,5,10,25]
	
	writes("Value of ", money, " can be changed by using a set of ")
	every writes(coins[1 to *coins], " ")
	write(" coins in ", count(coins, money), " different ways.")
end


  

You may also check:How to resolve the algorithm Sort an integer array step by step in the Ring programming language
You may also check:How to resolve the algorithm Abundant odd numbers step by step in the Swift programming language
You may also check:How to resolve the algorithm Median filter step by step in the Ada programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Call an object method step by step in the Clojure programming language