Imports System
Imports System.Collections.Generic
Public Class Program
Shared Sub GetCombinationsEqualToN(ByVal arr As List(Of Integer), ByVal N As Integer, ByVal combination As List(Of Integer))
Dim sum As Integer = 0
For Each num As Integer In combination
sum += num
Next
If sum = N Then
Console.WriteLine("sum(" & String.Join(",", combination.ToArray()) & ") = " & N)
End If
If sum >= N Then
Return
End If
For i As Integer = 0 To arr.Count - 1
Dim remaining As List(Of Integer) = New List(Of Integer)()
For j As Integer = i + 1 To arr.Count - 1
remaining.Add(arr(j))
Next
Dim combination_next As List(Of Integer) = New List(Of Integer)(combination)
combination_next.Add(arr(i))
GetCombinationsEqualToN(remaining, N, combination_next)
Next
End Sub
Public Shared Sub Main(ByVal args As String())
Dim list As List(Of Integer) = New List(Of Integer)() From {4, 6, 8, 2, 1, 10, 3, 5, 13}
Dim N As Integer = 13
GetCombinationsEqualToN(list, N, New List(Of Integer)())
End Sub
End Class
' run:
'
' sum(4,6,2,1) = 13
' sum(4,6,3) = 13
' sum(4,8,1) = 13
' sum(4,1,3,5) = 13
' sum(6,2,5) = 13
' sum(8,2,3) = 13
' sum(8,5) = 13
' sum(2,1,10) = 13
' sum(10,3) = 13
' sum(13) = 13
'