Find minimum number of coins that make a given value: Difference between revisions

Added Lua
(Added Algol 68)
(Added Lua)
Line 950:
(1, (2, 1))
(0, (1, 1))
</pre>
 
=={{header|Lua}}==
{{Trans|Wren}}
<syntaxhighlight lang="lua">
do -- find the minimum number of coins needed to make a given value
-- translated from the Wren sample
 
local denoms = { 200, 100, 50, 20, 10, 5, 2, 1 }
local amount = 988;
local coins, remaining = 0, amount
print( "The minimum number of coins needed to make a value of "..amount.." is as follows:" )
for _, denom in pairs( denoms ) do
local n = math.floor( remaining / denom )
if n > 0 then
coins = coins + n
print( string.format( "%6d", denom ).." x "..n )
remaining = remaining % denom
end
if remaining == 0 then break end
end
print( "A total of "..coins.." coins in all." )
end
</syntaxhighlight>
{{out}}
<pre>
The minimum number of coins needed to make a value of 988 is as follows:
200 x 4
100 x 1
50 x 1
20 x 1
10 x 1
5 x 1
2 x 1
1 x 1
A total of 11 coins in all.
</pre>
 
3,032

edits