How to resolve the algorithm Count the coins step by step in the REXX programming language
How to resolve the algorithm Count the coins step by step in the REXX 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 REXX programming language
Source code in the rexx programming language
/*REXX program counts the number of ways to make change with coins from an given amount.*/
numeric digits 20 /*be able to handle large amounts of $.*/
parse arg N $ /*obtain optional arguments from the CL*/
if N='' | N="," then N= 100 /*Not specified? Then Use $1 (≡100¢).*/
if $='' | $="," then $= 1 5 10 25 /*Use penny/nickel/dime/quarter default*/
if left(N, 1)=='$' then N= 100 * substr(N, 2) /*the count was specified in dollars. */
coins= words($) /*the number of coins specified. */
NN= N; do j=1 for coins /*create a fast way of accessing specie*/
_= word($, j) /*define an array element for the coin.*/
if _=='1/2' then _=.5 /*an alternate spelling of a half-cent.*/
if _=='1/4' then _=.25 /* " " " " " quarter-¢.*/
$.j= _ /*assign the value to a particular coin*/
end /*j*/
_= n//100; cnt=' cents' /* [↓] is the amount in whole dollars?*/
if _=0 then do; NN= '$' || (NN%100); cnt= /*show the amount in dollars, not cents*/
end /*show the amount in dollars, not cents*/
say 'with an amount of ' comma(NN)cnt", there are " comma( MKchg(N, coins) )
say 'ways to make change with coins of the following denominations: ' $
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _; n= _'.9'; #= 123456789; b= verify(n, #, "M")
e= verify(n, #'0', , verify(n, #"0.", 'M')) - 4
do j=e to b by -3; _= insert(',', _, j); end /*j*/; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
MKchg: procedure expose $.; parse arg a,k /*this function is invoked recursively.*/
if a==0 then return 1 /*unroll for a special case of zero. */
if k==1 then return 1 /* " " " " " " unity. */
if k==2 then f= 1 /*handle this special case of two. */
else f= MKchg(a, k-1) /*count, and then recurse the amount. */
if a==$.k then return f+1 /*handle this special case of A=a coin.*/
if a <$.k then return f /* " " " " " A
return f+MKchg(a-$.k,k) /*use diminished amount ($) for change.*/
/*REXX program counts the number of ways to make change with coins from an given amount.*/
numeric digits 20 /*be able to handle large amounts of $.*/
parse arg N $ /*obtain optional arguments from the CL*/
if N='' | N="," then N= 100 /*Not specified? Then Use $1 (≡100¢).*/
if $='' | $="," then $= 1 5 10 25 /*Use penny/nickel/dime/quarter default*/
if left(N,1)=='$' then N= 100 * substr(N, 2) /*the amount was specified in dollars.*/
NN= N; coins= words($) /*the number of coins specified. */
!.= .; do j=1 for coins /*create a fast way of accessing specie*/
_= word($, j); ?= _ ' coin' /*define an array element for the coin.*/
if _=='½' | _=="1/2" then _= .5 /*an alternate spelling of a half─cent.*/
if _=='¼' | _=="1/4" then _= .25 /* " " " " " quarter─¢.*/
$.j= _ /*assign the value to a particular coin*/
end /*j*/
_= n // 100; cnt=' cents' /* [↓] is the amount in whole dollars?*/
if _=0 then do; NN= '$' || (NN%100); cnt= /*show the amount in dollars, not cents*/
end
say 'with an amount of ' comma(NN)cnt", there are " comma( MKchg(N, coins) )
say 'ways to make change with coins of the following denominations: ' $
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _; n= _'.9'; #= 123456789; b= verify(n, #, "M")
e= verify(n, #'0', , verify(n, #"0.", 'M')) - 4
do j=e to b by -3; _= insert(',', _, j); end /*j*/; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
MKchg: procedure expose $. !.; parse arg a,k /*function is recursive. */
if !.a.k\==. then return !.a.k /*found this A & K before? */
if a==0 then return 1 /*unroll for a special case*/
if k==1 then return 1 /* " " " " " */
if k==2 then f= 1 /*handle this special case.*/
else f= MKchg(a, k-1) /*count, recurse the amount*/
if a==$.k then do; !.a.k= f+1; return !.a.k; end /*handle this special case.*/
if a <$.k then do; !.a.k= f ; return f ; end /* " " " " */
!.a.k= f + MKchg(a-$.k, k); return !.a.k /*compute, define, return. */
/*REXX program counts the number of ways to make change with coins from an given amount.*/
numeric digits 20 /*be able to handle large amounts of $.*/
parse arg N $ /*obtain optional arguments from the CL*/
if N='' | N="," then N= 100 /*Not specified? Then Use $1 (≡100¢).*/
if $='' | $="," then $= 1 5 10 25 /*Use penny/nickel/dime/quarter default*/
X= N /*save original for possible error msgs*/
if left(N,1)=='$' then do /*the amount has a leading dollar sign.*/
_= substr(N, 2) /*the amount was specified in dollars.*/
if \isNum(_) then call ser "amount isn't numeric: " N
N= 100 * _ /*change amount (in $) ───► cents (¢).*/
end
max$= 10 ** digits() /*the maximum amount this pgm can have.*/
if \isNum(N) then call ser X " amount isn't numeric."
if N=0 then call ser X " amount can't be zero."
if N<0 then call ser X " amount can't be negative."
if N>max$ then call ser X " amount can't be greater than " max$'.'
coins= words($); !.= .; NN= N; p= 0 /*#coins specified; coins; amount; prev*/
@.= 0 /*verify a coin was only specified once*/
do j=1 for coins; _= word($, j) /*create a fast way of accessing specie*/
?= _ ' coin' /*define an array element for the coin.*/
if _=='½' | _=="1/2" then _= .5 /*an alternate spelling of a half─cent.*/
if _=='¼' | _=="1/4" then _= .25 /* " " " " " quarter─¢.*/
if \isNum(_) then call ser ? "coin value isn't numeric."
if _<0 then call ser ? "coin value can't be negative."
if _<=0 then call ser ? "coin value can't be zero."
if @._ then call ser ? "coin was already specified."
if _
if _>N then call ser ? "coin must be less or equal to amount:" X
@._= 1; p= _ /*signify coin was specified; set prev.*/
$.j= _ /*assign the value to a particular coin*/
end /*j*/
_= n // 100; cnt= ' cents' /* [↓] is the amount in whole dollars?*/
if _=0 then do; NN= '$' || (NN%100); cnt= /*show the amount in dollars, not cents*/
end
say 'with an amount of ' comma(NN)cnt", there are " comma( MKchg(N, coins) )
say 'ways to make change with coins of the following denominations: ' $
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
isNum: return datatype(arg(1), 'N') /*return 1 if arg is numeric, 0 if not.*/
ser: say; say '***error***'; say; say arg(1); say; exit 13 /*error msg.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; parse arg _; n= _'.9'; #= 123456789; b= verify(n, #, "M")
e= verify(n, #'0', , verify(n, #"0.", 'M')) - 4
do j=e to b by -3; _= insert(',', _, j); end /*j*/; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
MKchg: procedure expose $. !.; parse arg a,k /*function is recursive. */
if !.a.k\==. then return !.a.k /*found this A & K before? */
if a==0 then return 1 /*unroll for a special case*/
if k==1 then return 1 /* " " " " " */
if k==2 then f= 1 /*handle this special case.*/
else f= MKchg(a, k-1) /*count, recurse the amount*/
if a==$.k then do; !.a.k= f+1; return !.a.k; end /*handle this special case.*/
if a <$.k then do; !.a.k= f ; return f ; end /* " " " " */
!.a.k= f + MKchg(a-$.k, k); return !.a.k /*compute, define, return. */
You may also check:How to resolve the algorithm Array concatenation step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Picat programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the Miranda programming language
You may also check:How to resolve the algorithm Knight's tour step by step in the Lua programming language
You may also check:How to resolve the algorithm Iterated digits squaring step by step in the J programming language