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

m
syntax highlighting fixup automation
(Add Red)
m (syntax highlighting fixup automation)
Line 21:
{{trans|Python_%3A%3A_Procedural}}
 
<langsyntaxhighlight lang="11l">V denominations = [1, 2, 5, 10, 20, 50, 100, 200]
V total = 988
print(‘Available denominations: ’denominations‘. Total is to be: ’total‘.’)
Line 28:
(V coinsused, remaining) = divmod(remaining, coins[n])
I coinsused > 0
print(‘ ’coinsused‘ * ’coins[n])</langsyntaxhighlight>
 
{{out}}
Line 44:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
DEFINE LEN="8"
BYTE ARRAY coins=[200 100 50 20 10 5 2 1],count(LEN)
Line 71:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_minimum_number_of_coins_that_make_a_given_value.png Screenshot from Atari 8-bit computer]
Line 88:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">coins←{
{⍺,≢⍵}⌸⍺[⍒⍺]{
coin←⊃(⍵≥⍺)/⍺
Line 94:
coin,⍺∇⍵-coin
}⍵
}</langsyntaxhighlight>
{{out}}
<pre> (1 2 5 10 20 50 100 200) coins 988
Line 107:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">----------------- MINIMUM NUMBER OF COINS ----------------
 
-- change :: [Int] -> Int -> [(Int, Int)]
Line 190:
set my text item delimiters to dlm
s
end unlines</langsyntaxhighlight>
{{Out}}
<pre>Summing to 1024:
Line 208:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">coins := [1, 2, 5, 10, 20, 50, 100, 200]
val := 988
 
Line 219:
}
MsgBox, 262144, , % result
return</langsyntaxhighlight>
{{out}}
<pre>4 * 200
Line 231:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_GIVEN_VALUE.AWK
BEGIN {
Line 254:
printf("%9d coins needed to disperse %s\n\n",total,arg1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 290:
 
=={{header|BASIC256}}==
<langsyntaxhighlight BASIC256lang="basic256">amount = 988
sumCoins = 0
dim coins = {1, 2, 5, 10, 20, 50, 100, 200}
Line 304:
end if
next n
end</langsyntaxhighlight>
{{out}}
<pre>
Line 312:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
//Find minimum number of coins that make a given value - Nigel Galloway: August 12th., 20
let fN g=let rec fG n g=function h::t->fG((g/h,h)::n)(g%h) t |_->n in fG [] g [200;100;50;20;10;5;2;1]
fN 988|>List.iter(fun(n,g)->printfn "Take %d of %d" n g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 331:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: assocs kernel math math.order prettyprint sorting ;
 
: make-change ( value coins -- assoc )
[ >=< ] sort [ /mod swap ] zip-with nip ;
 
988 { 1 2 5 10 20 50 100 200 } make-change .</langsyntaxhighlight>
{{out}}
<pre>
Line 353:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define floor(x) ((x*2.0-0.5) Shr 1)
 
Dim As Integer amount = 988
Line 370:
End If
Next n
Sleep</langsyntaxhighlight>
{{out}}
<pre>Make a value of 988 using the coins 1, 2, 5, 10, 20, 50, 100 and 200:
Line 385:
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 407:
}
fmt.Println("\nA total of", coins, "coins in all.")
}</langsyntaxhighlight>
 
{{out}}
Line 425:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (mapAccumL)
import Data.Tuple (swap)
 
Line 440:
main =
mapM_ print $
change [200, 100, 50, 20, 10, 5, 2, 1] 988</langsyntaxhighlight>
{{Out}}
<pre>(4,200)
Line 453:
Or as a hand-written recursion, defining a slightly more parsimonious listing, and allowing for denomination lists which are ill-sorted or incomplete.
 
<langsyntaxhighlight lang="haskell">import Data.List (sortOn)
import Data.Ord (Down (Down))
 
Line 495:
)
)
(change [200, 100, 50, 20, 10, 5, 2, 1] n)</langsyntaxhighlight>
{{Out}}
<pre>Summing to 1024:
Line 514:
=={{header|JavaScript}}==
{{Works with|JavaScript|ES6}}
<langsyntaxhighlight lang="javascript">(() => {
"use strict";
 
Line 578:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Summing to 1024:
Line 598:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
# If $details then provide {details, coins}, otherwise just the number of coins.
def minimum_number($details):
Line 626:
| minimum_number(false), # illustrate minimal output
task # illustrate detailed output
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 647:
=== Long version ===
Using a linear optimizer for this is serious overkill, but why not?
<langsyntaxhighlight lang="julia">using JuMP, GLPK
 
model = Model(GLPK.Optimizer)
Line 675:
println("Value of ", string(val), " is ", value(val))
end
</langsyntaxhighlight>{{out}}
<pre>
Optimized total coins: 11.0
Line 702:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">coins = {1, 2, 5, 10, 20, 50, 100, 200};
out = v /. ConvexOptimization[Total[v], coins . v == 988, v \[Element] Vectors[8, NonNegativeIntegers]];
MapThread[Row[{#1, " x ", #2}] &, {out, coins}] // Column</langsyntaxhighlight>
{{out}}
<pre>1 x 1
Line 716:
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Find minimum number of coins that make a given value. Nigel Galloway, August 11th., 2021
int: N=988;
Line 723:
solve minimize sum(n in 1..8)(take[n]);
output(["Take "++show(take[n])++" of "++show(coinValue[n])++"\n" | n in 1..8])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 740:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strformat
 
const
Line 757:
if remaining == 0: break
 
echo "\nTotal: ", count</langsyntaxhighlight>
 
{{out}}
Line 773:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 788:
for (0 .. $#amounts) {
printf "%1d * %3d\n", $amounts[$_], $denominations[$_]
}</langsyntaxhighlight>
{{out}}
<pre>4 * 200
Line 800:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.1"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (lastdelim added to the join() function)</span>
Line 819:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s coins were used.\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">proper</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">ordinal</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 836:
=={{header|Python}}==
===Python :: Procedural===
<langsyntaxhighlight lang="python">def makechange(denominations = [1,2,5,10,20,50,100,200], total = 988):
print(f"Available denominations: {denominations}. Total is to be: {total}.")
coins, remaining = sorted(denominations, reverse=True), total
Line 845:
 
makechange()
</langsyntaxhighlight>{{out}}
<pre>
Available denominations: [1, 2, 5, 10, 20, 50, 100, 200]. Total is to be: 988.
Line 859:
 
===Python :: Functional===
<langsyntaxhighlight lang="python">'''Minimum number of coins to make a given value'''
 
 
Line 899:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Summing to 1024:
Line 919:
Since unit denominations are possible, don't bother to check to see if an exact pay-out isn't possible.
 
<syntaxhighlight lang="raku" perl6line>my @denominations = 200, 100, 50, 20, 10, 5, 2, 1;
 
sub change (Int $n is copy where * >= 0) { gather for @denominations { take $n div $_; $n %= $_ } }
Line 926:
say "\n$amount:";
printf "%d × %d\n", |$_ for $amount.&change Z @denominations;
}</langsyntaxhighlight>
{{out}}
<pre>988:
Line 969:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
value: 988
Line 976:
unless 0 = quantity [print [quantity "*" denomination]]
value: value % denomination
]</langsyntaxhighlight>
{{out}}
<pre>
Line 993:
 
The total number of coins paid out is also shown.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds & displays the minimum number of coins which total to a specified value*/
parse arg $ coins /*obtain optional arguments from the CL*/
if $='' | $="," then $= 988 /*Not specified? Then use the default.*/
Line 1,020:
say 'number of coins dispensed: ' koins
if $>0 then say 'exact payout not possible.' /*There a residue? Payout not possible*/
exit 0 /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,041:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,062:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,080:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
fn main() {
let denoms = vec![200, 100, 50, 20, 10, 5, 2, 1];
Line 1,100:
println!("\nA total of {} coins in all.", coins);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,119:
{{libheader|Wren-fmt}}
As there is, apparently, an unlimited supply of coins of each denomination, it follows that any amount can be made up.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var denoms = [200, 100, 50, 20, 10, 5, 2, 1]
Line 1,135:
}
}
System.print("\nA total of %(coins) coins in all.")</langsyntaxhighlight>
 
{{out}}
Line 1,154:
=={{header|XPL0}}==
{{trans|Wren}}
<langsyntaxhighlight XPL0lang="xpl0">int Denom, Denoms, Coins, Amount, Remaining, I, N;
[Denoms:= [200, 100, 50, 20, 10, 5, 2, 1];
Coins:= 0;
Line 1,176:
A total of "); IntOut(0, Coins); Text(0, " coins in all.
");
]</langsyntaxhighlight>
 
{{out}}
Line 1,195:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">amount = 988
sumCoins = 0
dim coins(7)
Line 1,217:
end if
next n
end</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits