Count the coins/0-1: Difference between revisions
Content added Content deleted
(Added Algol 68) |
(Added FreeBasic) |
||
Line 231: | Line 231: | ||
Number of ways - order important : 3782932 (all perms of above indices) |
Number of ways - order important : 3782932 (all perms of above indices) |
||
</pre> |
</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}}== |
=={{header|Go}}== |