Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any Mathmeticians??

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
This is one for the math guys who code. I have a situation where I need to take a field in a group of records and see if any combination of the values in the field equal zero.

Example:

MyField
5
-2
7
-3
6

This group of records would have a combination that equals zero (5, -2, -3). I hope someone has something.

ProDev, MS Access Applications
Visit me at ==> Contact me at ==>lonniejohnson@prodev.us

May God bless you beyond your imagination!!!
 
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
 
This seems to me like a classic "knapsack" problem (see for more info).

One solution has been posted by someone called Enlade at
Note - there are other solutions to this. None of them are especially straightforward and there's no magic bullet [shadessad] but the above links should get you on the right road. [shadeshappy]

[pc2]
 
While potentially interesting, there are a some fuzzy issues to be clarified:

'Any combination' refers to two or More elements?

The example uses only integer values, is this actually part of the problem statement. Are 'real' values (also) possible in the set?

The example uses ADDITION, is this the only operator, or are other (subtraction; multiplication; etc) also allowed. If other operators are allowed, can they be used in combination(s). If combinatios of operators are allowed, is there some limitation? If limited multiplae operators are considered, are there some limits to the number of operators and / or the complexity of the expression(s) to be considered?

A slight variation of many of these issues are illustrated by the 'childrens' math game "24". The game is (was?) available to help teach simple math functions to elementary school age children. It has some limits and variation of your problem but does permit manyb of the variations (issues) mentioned above.

Finally, I would like to know what would be the application of this (somewhat unusual) procedure?





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top