Count the coins: Difference between revisions

(Output minimal D version)
Line 441:
amount, ways to make change: 100000 13398445413854501
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>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<lang>
 
This is a naive implementation and very slow.
<lang Icon>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</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf.icn printf.icn provides formatting]
 
Output:<pre>There are 6 ways to count change for 15 using [ 1 5 10 25 ] coins.
There are 242 ways to count change for 100 using [ 1 5 10 25 ] coins.
^c</pre>
 
 
=={{header|J}}==
Anonymous user