Solved

greatest number of numbers that sums to n

Posted on 2004-08-07
2
176 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

912 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now