Count the coins/0-1: Difference between revisions

m
(J)
 
(8 intermediate revisions by 6 users not shown)
Line 26:
 
*  Show an example of coins you used to reach the given sum and their indices. See Raku for this case.
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">T Solver
Int want, width
count1 = 0
count2 = 0
 
F (want, width)
.want = want
.width = width
 
F count(Int sum; [Int] used, have, uindices, rindices) -> Void
I sum == .want
.count1++
.count2 += factorial(used.len)
I .count1 < 11
V uindiceStr = uindices.join(‘ ’).ljust(.width)
print(‘ indices ’uindiceStr‘ => used ’used.join(‘ ’))
E I sum < .want & !have.empty
V thisCoin = have[0]
V index = rindices[0]
V rest = have[1..]
V new_rindices = rindices[1..]
.count(sum + thisCoin, used [+] thisCoin, rest, uindices [+] index, new_rindices)
.count(sum, used, rest, uindices, new_rindices)
 
F count_coins(Int want, [Int] &coins, Int width)
print(‘Sum ’want‘ from coins ’coins.join(‘ ’))
V solver = Solver(want, width)
V rindices = Array(0 .< coins.len)
solver.count(0, [Int](), coins, [Int](), rindices)
I solver.count1 > 10
print(‘ .......’)
print(‘ (only the first 10 ways generated are shown)’)
print(‘Number of ways - order unimportant : ’(solver.count1)‘ (as above)’)
print(‘Number of ways - order important : ’(solver.count2)" (all perms of above indices)\n")
 
count_coins(6, &[1, 2, 3, 4, 5], 5)
count_coins(6, &[1, 1, 2, 3, 3, 4, 5], 7)
count_coins(40, &[1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100], 18)</syntaxhighlight>
 
{{out}}
<pre>
Sum 6 from coins 1 2 3 4 5
indices 0 1 2 => used 1 2 3
indices 0 4 => used 1 5
indices 1 3 => used 2 4
Number of ways - order unimportant : 3 (as above)
Number of ways - order important : 10 (all perms of above indices)
 
Sum 6 from coins 1 1 2 3 3 4 5
indices 0 1 5 => used 1 1 4
indices 0 2 3 => used 1 2 3
indices 0 2 4 => used 1 2 3
indices 0 6 => used 1 5
indices 1 2 3 => used 1 2 3
indices 1 2 4 => used 1 2 3
indices 1 6 => used 1 5
indices 2 5 => used 2 4
indices 3 4 => used 3 3
Number of ways - order unimportant : 9 (as above)
Number of ways - order important : 38 (all perms of above indices)
 
Sum 40 from coins 1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100
indices 0 1 2 3 4 5 6 7 10 => used 1 2 3 4 5 5 5 5 10
indices 0 1 2 3 4 5 6 7 11 => used 1 2 3 4 5 5 5 5 10
indices 0 1 2 3 4 5 6 7 12 => used 1 2 3 4 5 5 5 5 10
indices 0 1 2 3 4 5 6 7 13 => used 1 2 3 4 5 5 5 5 10
indices 0 1 2 3 4 5 6 8 => used 1 2 3 4 5 5 5 15
indices 0 1 2 3 4 5 6 9 => used 1 2 3 4 5 5 5 15
indices 0 1 2 3 4 5 7 8 => used 1 2 3 4 5 5 5 15
indices 0 1 2 3 4 5 7 9 => used 1 2 3 4 5 5 5 15
indices 0 1 2 3 4 5 10 11 => used 1 2 3 4 5 5 10 10
indices 0 1 2 3 4 5 10 12 => used 1 2 3 4 5 5 10 10
.......
(only the first 10 ways generated are shown)
Number of ways - order unimportant : 464 (as above)
Number of ways - order important : 3782932 (all perms of above indices)
 
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Go}}which is{{Trans|Wren}}
<syntaxhighlight lang="algol68">
BEGIN # count the ways of summing coins to a particular number - translation of the Go sample #
INT countu := 0; # order unimportant #
INT counti := 0; # order important #
INT width := 0; # for printing purposes #
 
PROC factorial = (INT n )INT:
BEGIN
INT prod := 1;
FOR i FROM 2 TO n DO
prod *:= i
OD;
prod
END # factorial # ;
 
OP LEN = ( []INT a )INT: ( UPB a - LWB a ) + 1;
OP LEN = ( STRING a )INT: ( UPB a - LWB a ) + 1;
PROC append = ( []INT a, INT b )[]INT:
BEGIN
[ 1 : LEN a + 1 ]INT result;
result[ 1 : LEN a ] := a;
result[ LEN a + 1 ] := b;
result
END # append # ;
OP TOSTRING = ( []INT a )STRING:
BEGIN
STRING result := "[";
STRING separator := "";
FOR i FROM LWB a TO UPB a DO
result +:= separator + whole( a[ i ], 0 );
separator := " "
OD;
result +:= "]"
END # TOSTRING # ;
PROC pad = ( STRING a, INT width )STRING:
IF INT len a = LEN a; len a >= width THEN a ELSE a + ( " " * ( width - len a ) ) FI;
PROC count = ( INT want, []INT used, INT sum, []INT have, uindices, rindices )VOID:
IF sum = want THEN
countu +:= 1;
counti +:= factorial( LEN used );
IF countu < 11 THEN
STRING uindices str = TOSTRING uindices;
print( ( " indices ", pad( uindices str, width ), " => used ", TOSTRING used, newline ) )
FI
ELIF sum < want AND LEN have /= 0 THEN
INT this coin = have[ LWB have ];
INT index = rindices[ LWB rindices ];
[]INT rest = have[ LWB have + 1 : ];
count( want, append( used, this coin ), sum + this coin, rest,
append( uindices, index ), rindices[ LWB rindices + 1 : ] );
count( want, used, sum, rest, uindices, rindices[ LWB rindices + 1 : ] )
FI # count # ;
 
PROC count coins = ( INT want, []INT coins, INT rqd width )VOID:
BEGIN
print( ( "Sum ", whole( want, 0 ), " from coins ", TOSTRING coins, newline ) );
countu := 0;
counti := 0;
width := rqd width;
[ 0 : LEN coins - 1 ]INT rindices;
FOR i FROM LWB rindices TO UPB rindices DO
rindices[ i ] := i
OD;
count( want, []INT(), 0, coins, []INT(), rindices );
IF countu > 10 THEN
print( ( " .......", newline ) );
print( ( " (only the first 10 ways generated are shown)", newline ) )
FI;
print( ( "Number of ways - order unimportant : ", whole( countu, 0 )
, " (as above)", newline ) );
print( ( "Number of ways - order important : ", whole( counti, 0 )
, " (all perms of above indices)", newline, newline ) )
END # count coins # ;
 
BEGIN # task #
count coins( 6, ( 1, 2, 3, 4, 5 ), 7 );
count coins( 6, ( 1, 1, 2, 3, 3, 4, 5 ), 9 );
count coins( 40, ( 1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100 ), 20 )
END
END
</syntaxhighlight>
{{out}}
<pre>
Sum 6 from coins [1 2 3 4 5]
indices [0 1 2] => used [1 2 3]
indices [0 4] => used [1 5]
indices [1 3] => used [2 4]
Number of ways - order unimportant : 3 (as above)
Number of ways - order important : 10 (all perms of above indices)
 
Sum 6 from coins [1 1 2 3 3 4 5]
indices [0 1 5] => used [1 1 4]
indices [0 2 3] => used [1 2 3]
indices [0 2 4] => used [1 2 3]
indices [0 6] => used [1 5]
indices [1 2 3] => used [1 2 3]
indices [1 2 4] => used [1 2 3]
indices [1 6] => used [1 5]
indices [2 5] => used [2 4]
indices [3 4] => used [3 3]
Number of ways - order unimportant : 9 (as above)
Number of ways - order important : 38 (all perms of above indices)
 
Sum 40 from coins [1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100]
indices [0 1 2 3 4 5 6 7 10] => used [1 2 3 4 5 5 5 5 10]
indices [0 1 2 3 4 5 6 7 11] => used [1 2 3 4 5 5 5 5 10]
indices [0 1 2 3 4 5 6 7 12] => used [1 2 3 4 5 5 5 5 10]
indices [0 1 2 3 4 5 6 7 13] => used [1 2 3 4 5 5 5 5 10]
indices [0 1 2 3 4 5 6 8] => used [1 2 3 4 5 5 5 15]
indices [0 1 2 3 4 5 6 9] => used [1 2 3 4 5 5 5 15]
indices [0 1 2 3 4 5 7 8] => used [1 2 3 4 5 5 5 15]
indices [0 1 2 3 4 5 7 9] => used [1 2 3 4 5 5 5 15]
indices [0 1 2 3 4 5 10 11] => used [1 2 3 4 5 5 10 10]
indices [0 1 2 3 4 5 10 12] => used [1 2 3 4 5 5 10 10]
.......
(only the first 10 ways generated are shown)
Number of ways - order unimportant : 464 (as above)
Number of ways - order important : 3782932 (all perms of above indices)
</pre>
 
=={{header|FreeBASIC}}==
Based on the Phix entry and the Nim entry
<syntaxhighlight lang="vb">#define factorial(n) Iif(n<2, 1, n*factorial1(n-1))
 
REM silly way
Function silly(coins() As Short, tgt As Short, cdx As Short = 1) As Integer
Dim As Integer count = 0
If tgt = 0 Then
count += 1
Elseif tgt > 0 And cdx <= Ubound(coins) Then
count += silly(coins(), tgt-coins(cdx), cdx+1)
count += silly(coins(), tgt, cdx+1)
End If
Return count
End Function
 
REM very silly way
Function very_silly(coins() As Short, tgt As Short, cdx As Short = 1, taken As Short = 0) As Integer
Dim As Integer count = 0
If tgt = 0 Then
count += factorial(taken)
Elseif tgt > 0 And cdx <= Ubound(coins) Then
count += very_silly(coins(), tgt-coins(cdx), cdx+1, taken+1)
count += very_silly(coins(), tgt, cdx+1, taken)
End If
Return count
End Function
 
Sub countCoins(coins() As Short, tgt As Short)
Print "Sum"; tgt; " from coins ";
For a As Short = 1 To Ubound(coins)
Print coins(a);
Next a
Print !"\nNumber of ways - order unimportant:"; silly(coins(), tgt)
Print "Number of ways - order important :"; very_silly(coins(), tgt); !"\n"
End Sub
 
Dim As Short test0(1 To ...) = {1, 2, 3, 4, 5}
countCoins(test0(), 6)
 
Dim As Short test1(1 To ...) = {1, 1, 2, 3, 3, 4, 5}
countCoins(test1(), 6)
 
Dim As Short test2(1 To ...) = {1,2,3,4,5,5,5,5,15,15,10,10,10,10,25,100}
countCoins(test2(), 40)
 
Sleep</syntaxhighlight>
{{out}}
<pre>Sum 6 from coins 1 2 3 4 5
Number of ways - order unimportant: 3
Number of ways - order important : 10
 
Sum 6 from coins 1 1 2 3 3 4 5
Number of ways - order unimportant: 9
Number of ways - order important : 38
 
Sum 40 from coins 1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100
Number of ways - order unimportant: 464
Number of ways - order important : 3782932</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 86 ⟶ 351:
countCoins(6, []int{1, 1, 2, 3, 3, 4, 5}, 9)
countCoins(40, []int{1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100}, 20)
}</langsyntaxhighlight>
 
{{out}}
Line 129 ⟶ 394:
=={{header|J}}==
 
Implementation:<langsyntaxhighlight Jlang="j">selcoins=: {{ #:I.x=(#:i.2^#y)+/ .*y }}
ccoins=: {{ (#i), +/!x:+/"1 i=. x selcoins y }}</langsyntaxhighlight>
 
<code>selcoins</code> identifies the valid selections of the coins. <code>ccoins</code> counts thenthem, giving two counts (first is where coin order does not matter, second is where each permutation of physical coins is counted separately).
 
Task examples:<langsyntaxhighlight Jlang="j"> 6 ccoins 1 2 3 4 5
3 10
6 ccoins 1 1 2 3 3 4 5
9 38
40 ccoins 1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100
464 3782932</langsyntaxhighlight>
 
Some example coin selections:<syntaxhighlight lang="j"> rplc&(' 0';'')"1":6 (]#~selcoins) 1 2 3 4 5
2 4
1 5
1 2 3
rplc&(' 0';'')"1":6 (]#~selcoins) 1 1 2 3 3 4 5
3 3
2 4
1 5
1 2 3
1 2 3
1 5
1 2 3
1 2 3
1 1 4
rplc&(' 0';'')"1":40 (]#~9{.selcoins) 1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100
10 10 10 10
15 25
15 25
15 15 10
15 15 10
15 15 10
15 15 10
5 10 25
5 10 25</syntaxhighlight>
 
=={{header|jq}}==
Line 147 ⟶ 437:
The solutions presented in this section use generic `combinations` and `permutations`
functions defined as follows:
<syntaxhighlight lang="jq">
<lang jq>
# Input: an array of arrays
# Output: a stream of arrays corresponding to the selection of exactly one item
Line 166 ⟶ 456:
| [.[$i]] + (del(.[$i])|permutations)
end ;
</syntaxhighlight>
</lang>
<syntaxhighlight lang="jq">
<lang jq>
## Generic helper functions:
def count(s): reduce s as $x (0; .+1);
Line 174 ⟶ 464:
# Output: [ [a,0], [b,0],... ] | combinations
def zero_or_one: [ .[] | [., 0] ] | combinations;
</syntaxhighlight>
</lang>
 
'''The task'''
<syntaxhighlight lang="jq">
<lang jq>
def count_combinations_with_sum($sum):
count( zero_or_one | select(add == $sum));
Line 197 ⟶ 487:
[[1, 1, 2, 3, 3, 4, 5], 6],
[[1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100], 40]
| task</langsyntaxhighlight>
{{out}}
<pre>
Line 214 ⟶ 504:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
function coinsum(coins, targetsum; verbose=true)
Line 235 ⟶ 525:
coinsum([1, 1, 2, 3, 3, 4, 5], 6)
coinsum([1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100], 40, verbose=false)
</langsyntaxhighlight>{{out}}
<pre>
Coins are [1, 2, 3, 4, 5], target sum is 6:
Line 308 ⟶ 598:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CoinSums]
CoinSums[coins_List, sum_] := Module[{c, len, max, bin, sel, out},
c = {Range[Length[coins]], coins} // Transpose;
Line 330 ⟶ 620:
CoinSums[{1, 2, 3, 4, 5}, 6]
CoinSums[{1, 1, 2, 3, 3, 4, 5}, 6]
CoinSums[{1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100}, 40] // Take[#, UpTo[10]] &</langsyntaxhighlight>
{{out}}
Note that the indexing is 1-based and that for the last case only the first 10 solutions are shown:
Line 342 ⟶ 632:
=={{header|MiniZinc}}==
; coins = [1, 2, 3, 4, 5] and sum = 6
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Subset sum. Nigel Galloway: January 6th., 2021.
enum Items={a,b,c,d,e};
Line 349 ⟶ 639:
var int: wSelected=sum(n in selected)(weight[n]);
constraint wSelected=6;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 373 ⟶ 663:
</pre>
; coins = [1, 1, 2, 3, 3, 4, 5] and sum = 6
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Subset sum. Nigel Galloway: January 6th., 2021.
enum Items={a,b,c,d,e,f,g};
Line 380 ⟶ 670:
var int: wSelected=sum(n in selected)(weight[n]);
constraint wSelected=6;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 416 ⟶ 706:
</pre>
; coins = [1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100] and sum = 40
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Subset sum. Nigel Galloway: January 6th., 2021.
enum Items={a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p};
Line 423 ⟶ 713:
var int: wSelected=sum(n in selected)(weight[n]);
constraint wSelected=40;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 476 ⟶ 766:
 
Using a solver object rather than global variables.
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strutils
 
type Solver = object
Line 513 ⟶ 803:
countCoins(6, [1, 2, 3, 4, 5], 5)
countCoins(6, [1, 1, 2, 3, 3, 4, 5], 7)
countCoins(40, [1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100], 18)</langsyntaxhighlight>
 
{{out}}
Line 554 ⟶ 844:
=={{header|Pascal}}==
first count all combinations by brute force.Order is important.Modified nQueens.<BR>Next adding weigths by index.<BR>Using Phix wording 'silly'.
<langsyntaxhighlight lang="pascal">program Coins0_1;
{$IFDEF FPC}
{$MODE DELPHI}
Line 665 ⟶ 955:
CheckAll(High(coins3),40);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 677 ⟶ 967:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Count_the_coins/0-1
Line 708 ⟶ 998:
count( $want, $used, $sum, \@rest);
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 724 ⟶ 1,014:
=== sane way ===
In the second test, this counts [1,2,3] as one way to get 6, not counting the other [1,2,3] or the other [1,2,3] or the other [1,2,3].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">choices</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">coins</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
Line 744 ⟶ 1,034:
<span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span><span style="color: #000000;">40</span><span style="color: #0000FF;">}}</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;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">choices</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 751 ⟶ 1,041:
=== silly way ===
In the second test, this counts [1,2,3] as four ways to get 6, since there are two 1s and two 3s... ("order unimportant")
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">silly</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">coins</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
Line 768 ⟶ 1,058:
<span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span><span style="color: #000000;">40</span><span style="color: #0000FF;">}}</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;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">silly</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 775 ⟶ 1,065:
=== very silly way ===
In the second test, this counts [1,2,3] as 24 ways to get 6, from 2 1s, 2 3s, and 6 ways to order them ("order important")
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">very_silly</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">coins</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">tgt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">taken</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
Line 792 ⟶ 1,082:
<span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">25</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">},</span><span style="color: #000000;">40</span><span style="color: #0000FF;">}}</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;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">very_silly</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 798 ⟶ 1,088:
</pre>
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import product, compress
 
fact = lambda n: n and n*fact(n - 1) or 1
Line 815 ⟶ 1,105:
for s, c in cases:
print(f'{combo_count(s, c, perm):7d} ways for {s:2d} total from coins {c}')
print()</langsyntaxhighlight>
{{out}}
<pre>Order matters: False
Line 832 ⟶ 1,122:
This is pretty much duplicating other tasks, in process if not wording. Even though I am adding a solution, my vote would be for deletion as it doesn't really add anything to the other tasks; [[Combinations]], [[Permutations]], [[Subset sum problem]] and to a large extent [[4-rings or 4-squares puzzle]].
 
<syntaxhighlight lang="raku" perl6line>for <1 2 3 4 5>, 6
,<1 1 2 3 3 4 5>, 6
,<1 2 3 4 5 5 5 5 15 15 10 10 10 10 25 100>, 40
Line 848 ⟶ 1,138:
put "\nOrder {$un}important:\nCount: { +$list }\nIndices" ~ ( +$list > 10 ?? ' (10 random examples):' !! ':' );
put $list.pick(10).sort».join(', ').join: "\n"
}</langsyntaxhighlight>
{{out}}
<pre>How many combinations of [1, 2, 3, 4, 5] sum to 6?
Line 938 ⟶ 1,228:
Well, after some huffing and puffing, the house is still standing so I thought I'd have a go at it.
Based on the Perl algorithm but modified to deal with the extra credit.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
 
var cnt = 0 // order unimportant
Line 977 ⟶ 1,267:
countCoins.call(6, [1, 2, 3, 4, 5], 9)
countCoins.call(6, [1, 1, 2, 3, 3, 4, 5], 9)
countCoins.call(40, [1, 2, 3, 4, 5, 5, 5, 5, 15, 15, 10, 10, 10, 10, 25, 100], 28)</langsyntaxhighlight>
 
{{out}}
1,480

edits