Find minimum number of coins that make a given value: Difference between revisions
Find minimum number of coins that make a given value (view source)
Revision as of 12:56, 27 August 2022
, 1 year agosyntax highlighting fixup automation
(Add Red) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 21:
{{trans|Python_%3A%3A_Procedural}}
<
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])</
{{out}}
Line 44:
=={{header|Action!}}==
<
DEFINE LEN="8"
BYTE ARRAY coins=[200 100 50 20 10 5 2 1],count(LEN)
Line 71:
FI
OD
RETURN</
{{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}}
<
{⍺,≢⍵}⌸⍺[⍒⍺]{
coin←⊃(⍵≥⍺)/⍺
Line 94:
coin,⍺∇⍵-coin
}⍵
}</
{{out}}
<pre> (1 2 5 10 20 50 100 200) coins 988
Line 107:
=={{header|AppleScript}}==
<
-- change :: [Int] -> Int -> [(Int, Int)]
Line 190:
set my text item delimiters to dlm
s
end unlines</
{{Out}}
<pre>Summing to 1024:
Line 208:
=={{header|AutoHotkey}}==
<
val := 988
Line 219:
}
MsgBox, 262144, , % result
return</
{{out}}
<pre>4 * 200
Line 231:
=={{header|AWK}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 290:
=={{header|BASIC256}}==
<
sumCoins = 0
dim coins = {1, 2, 5, 10, 20, 50, 100, 200}
Line 304:
end if
next n
end</
{{out}}
<pre>
Line 312:
=={{header|F_Sharp|F#}}==
<
//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>
{{out}}
<pre>
Line 331:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
: make-change ( value coins -- assoc )
[ >=< ] sort [ /mod swap ] zip-with nip ;
988 { 1 2 5 10 20 50 100 200 } make-change .</
{{out}}
<pre>
Line 353:
=={{header|FreeBASIC}}==
<
Dim As Integer amount = 988
Line 370:
End If
Next n
Sleep</
{{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}}
<
import "fmt"
Line 407:
}
fmt.Println("\nA total of", coins, "coins in all.")
}</
{{out}}
Line 425:
=={{header|Haskell}}==
<
import Data.Tuple (swap)
Line 440:
main =
mapM_ print $
change [200, 100, 50, 20, 10, 5, 2, 1] 988</
{{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.
<
import Data.Ord (Down (Down))
Line 495:
)
)
(change [200, 100, 50, 20, 10, 5, 2, 1] n)</
{{Out}}
<pre>Summing to 1024:
Line 514:
=={{header|JavaScript}}==
{{Works with|JavaScript|ES6}}
<
"use strict";
Line 578:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Summing to 1024:
Line 598:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight 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>
{{out}}
<pre>
Line 647:
=== Long version ===
Using a linear optimizer for this is serious overkill, but why not?
<
model = Model(GLPK.Optimizer)
Line 675:
println("Value of ", string(val), " is ", value(val))
end
</
<pre>
Optimized total coins: 11.0
Line 702:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
out = v /. ConvexOptimization[Total[v], coins . v == 988, v \[Element] Vectors[8, NonNegativeIntegers]];
MapThread[Row[{#1, " x ", #2}] &, {out, coins}] // Column</
{{out}}
<pre>1 x 1
Line 716:
=={{header|MiniZinc}}==
<syntaxhighlight 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>
{{out}}
<pre>
Line 740:
=={{header|Nim}}==
<
const
Line 757:
if remaining == 0: break
echo "\nTotal: ", count</
{{out}}
Line 773:
=={{header|Perl}}==
<
use warnings;
Line 788:
for (0 .. $#amounts) {
printf "%1d * %3d\n", $amounts[$_], $denominations[$_]
}</
{{out}}
<pre>4 * 200
Line 800:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 836:
=={{header|Python}}==
===Python :: Procedural===
<
print(f"Available denominations: {denominations}. Total is to be: {total}.")
coins, remaining = sorted(denominations, reverse=True), total
Line 845:
makechange()
</
<pre>
Available denominations: [1, 2, 5, 10, 20, 50, 100, 200]. Total is to be: 988.
Line 859:
===Python :: Functional===
<
Line 899:
# MAIN ---
if __name__ == '__main__':
main()</
{{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"
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;
}</
{{out}}
<pre>988:
Line 969:
=={{header|Red}}==
<
value: 988
Line 976:
unless 0 = quantity [print [quantity "*" denomination]]
value: value % denomination
]</
{{out}}
<pre>
Line 993:
The total number of coins paid out is also shown.
<
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. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,041:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 1,062:
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,080:
=={{header|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>
{{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.
<
var denoms = [200, 100, 50, 20, 10, 5, 2, 1]
Line 1,135:
}
}
System.print("\nA total of %(coins) coins in all.")</
{{out}}
Line 1,154:
=={{header|XPL0}}==
{{trans|Wren}}
<
[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.
");
]</
{{out}}
Line 1,195:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<
sumCoins = 0
dim coins(7)
Line 1,217:
end if
next n
end</
{{out}}
<pre>
|