Solved

greatest number of numbers that sums to n

Posted on 2004-08-07
2
177 Views
Last Modified: 2012-05-05
Hi.  I am trying to make a program that takes in a list box (lets call it list1) with a bunch of items in it that have a numerical value.  Next, I have 4 other numbers in a different list.  The point of this is to take a number from the list2 and display all the possibilities in which when you sum up that first card with any number of cards from list1, the total is exactly 11.

Can anyone make me a function that does this? Thank you.
0
Comment
Question by:cachedVB
2 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 80 total points
ID: 11745822
This creates two string arrays.
Indices will show the combination in terms of the list positions of the parts, e.g. "1, 3 ,4"
Values shows the component parts including the number from list2 e.g. "1 + 5 + 5"

Private Sub GetCombinations(List2Number as integer)
Dim Indices() As String
Dim Values() As String
Dim CombinationCount As Integer
Dim Total As Integer
Dim i As Integer
Dim j As Integer
Dim Value As String
Dim Index As String

For i = 1 To (2 ^ List1.ListCount)
    Value = List2Number
    Index = ""
    Total = List2Number
    For j = 0 To List1.ListCount - 1
        If 2 ^ j And i Then
            Total = Total + List1.List(j)
            Index = Index & IIf(Index = "", "", ", ") & j
            Value = Value & " + " & List1.List(j)
        End If
        Next j
    If Total = 11 Then
        ReDim Preserve Indices(CombinationCount)
        ReDim Preserve Values(CombinationCount)
        Indices(CombinationCount) = Index
        Values(CombinationCount) = Value
        CombinationCount = CombinationCount + 1
    End If
Next i
End Sub
0
 
LVL 4

Author Comment

by:cachedVB
ID: 11746340
Thanks! Just what I wanted... although I had to tweak it a bit to find the longest one. :)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question