Lonnie,
If there were a static number of candidates, it would be
very simple to make a set of nested loops where each
loop index ranges from 0 to 1 and is multiplied by the
corresponding element. It would not matter if there were
5, 10 or whatever. It is just a more deeply nested loop.
Code:
For i = 0 To 1
For j = 0 To 1
For k = 0 To 1
If Nums(1) * i + Nums(2) * j + Nums(3) * k = 0 Then
' The subscripts = 1 are the valid combination(s)
Next k
Next j
Next i
This situation calls for a recursive approach:
Unfortunately, I have written my recursive procedure for
the decade. This is a different sort of problem, because
the search does not terminate when an answer is found.
I think the result is a global, multi-dimensional variant
that the search routine keeps adding to.
Here is an example of a recursive routine put together for
an entirely different problem:
Rats, now you have me thinking about this. I'm thinking
that there may still be a very easy approach with a two-
dimensional array. One for the numbers and the second
for the (Times 0/1) like above. Now the problem is just
how to get the second subscript to toggle every possible
combination of 0/1 and for each iteration add them up.
OK, now I'll sleep tonight. This just reads a recordset
into Nums (i, 1). Then it is going to treat Nums (i,2)
as a "vertical binary number". For each possible value
to 2 ^ TotalNums it will populate the binary value,
do the multiplication (by one or zero) and then sum
the combined column). It is by no means perfect but
I think it works.
Code:
Dim Nums(100, 2) As Integer
Dim TotalNums As Integer
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Dim ThePower As Integer
Dim LeftOver As Integer
Dim i As Integer
Dim Temp As Integer
Stop
Set dbs = CurrentDb
sql = "Select * from TheNumbers"
Set rst = dbs.OpenRecordset(sql)
' Read them in and count
TotalNums = 1
While Not rst.EOF And Not rst.BOF
Nums(TotalNums, 1) = rst!TheNumber
TotalNums = TotalNums + 1
rst.MoveNext
Wend
TotalNums = TotalNums - 1
' Now try every combination
For i = 1 To 2 ^ TotalNums
' Break i down into binary
Temp = i
For j = TotalNums To 0 Step -1
If Temp >= 2 ^ j Then
Nums(j, 2) = 1
Temp = Temp - 2 ^ j
Else
Nums(j, 2) = 0
End If
Next j
' Now check the sum of (columns 1 * 2)
Temp = 0
For j = 1 To TotalNums
Temp = Temp + Nums(j, 1) * Nums(j, 2)
Next j
If Temp = 0 Then
MsgBox ("Found 1")
End If
Next i
Wayne