Count the coins: Difference between revisions

m
→‎{{header|Perl 6}}: one code block
m (→‎{{header|Perl 6}}: one code block)
Line 1,937:
{{works with|rakudo|2015.09}}
{{trans|Ruby}}
 
===Recursive (cached)===
<lang perl6>sub# Recursive ways-to-make-change($amount, @coinscached) {
sub change-r($amount, @coins) {
my @cache = $[1 xx @coins];
Line 1,946 ⟶ 1,947:
multi ways($,@) { 0 }
# more efficient to start with coins sorted in descending order
ways($amount, @coins.sort(-*).list); # sort descending
}
say wayschange-to-make-changer 1_00, [1,5,10,25];
say wayschange-to-make-changer 1000_00, [1,5,10,25,50,100];</lang>
 
{{out}}
===# Iterative===
<pre>242
<lang perl6>sub ways-to-make-change-slowlyi(\n, @coins) {
13398445413854501</pre>
===Iterative===
<lang perl6>sub ways-to-make-change-slowly(\n, @coins) {
my @table = [1 xx @coins], [0 xx @coins] xx n;
for 1..n X ^@coins -> (\i, \j) {
Line 1,966:
}
 
say ways-to-make-change-slowlyi 1_00, [1,5,10,25];
say ways-to-make-change-slowlyi 1000_00, [1,5,10,25,50,100];</lang>
Both versions produce the same output.
{{out}}
<pre>242
13398445413854501</pre>
 
=={{header|Phix}}==
2,392

edits