Marvin --
Glad that works for you! It was a fun problem to address, and, as you can see from the discussion you provoked, appropriately titled...
Jay
>Wow Jay...this works. I'm not sure I totally understand it. My mind doesn't quite work that way. But so far this works perfectly with very little modification. Thank You for doing my job for me!!!
>
>>>I'm working on a reconciliation project and i'm stuck. I have a list of numbers lets say 100 numbers and I have a target amount that some permutation of these numbers adds up to. I don't know how many of them will add up to this number. I need the list of numbers that adds up to my target number. I tried writing this but my brain overworked itself. Anybody ever write something like this or have any ideas? Thanks in advance!!!
>>>
>>>Example
>>>Say my number is 362.21 and my number list is:
>>>201.10
>>>25.18
>>>150.11
>>>778.88
>>>151.21
>>>11.31
>>>10.00
>>>...
>>>The answer is 201.10 150.11 and 10.00
>>
>>Marvin --
>>
>>Another solution, which is more scaleable, and is simple.
>>
>>The hard part of this problem is all that's needed to exhaustively test the combinations.
>>
>>But, there is a simple mechanism for us already. Consider the binary numbering system. Let's map each digit of a binary number of 100 digits to the 100 values that you need. Then we generate every number between 0 and 2 to the 100th. If we consider a binary digit as an on/off switch -- on to add the respective value to the sum, off to ignore it, we will generate all possible combinations of the values you have. For example, if you have 100.5 as the first value. If the 1st digit of our binary number is 1, then we add the value to the sum. If the 1st digit of our binary number is 0, we ignore it.
>>
>>Then, to cycle through all the combinations all we have to do is start a mask value with 0 and increment by 1, add the values identified by the mask for that number, and cycle again until we reach the last number!
>>
>>Below is a program which does just that:
>>
>>
>>LOCAL llContinue, llAdd, llCarry, X, lnSum, lcValues
>>
>>#DEFINE nMASK 1
>>#DEFINE nVALUE 2
>>#DEFINE nVALUES 10
>>#DEFINE nTARGET 15
>>
>>DIMENSION aCombo [nVALUES, 2]
>>
>>aCombo [1, nValue] = 3
>>aCombo [2, nValue] = 6
>>aCombo [3, nValue] = 7
>>aCombo [4, nValue] = 2
>>aCombo [5, nValue] = 1
>>aCombo [6, nValue] = 4
>>aCombo [7, nValue] = 13
>>aCombo [8, nValue] = 12
>>aCombo [9, nValue] = 9
>>aCombo [10, nValue] = 10
>>
>>llContinue = .T.
>>DO WHILE llContinue
>> * Test
>> lnSum = 0
>> FOR X = 1 TO nVALUES
>> lnSum = lnSum ;
>> + IIF (aCombo[X, nMask],;
>> aCombo[X, nValue], 0)
>> ENDFOR
>>
>> * Output
>> lcValues = ""
>> IF lnSum = nTARGET
>> FOR X = 1 TO nVALUES
>> lcValues = lcValues ;
>> + IIF (aCombo[X, nMask], ;
>> TRANSFORM (aCombo[X, nValue]) + ",", ;
>> "")
>> ENDFOR
>> lcValues = LEFT (lcValues, LEN (lcValues) - 1)
>> WAIT WINDOW lcValues
>> ENDIF
>>
>> * Increment mask
>>
>> llAdd = .T.
>> FOR X = 1 TO nVALUES
>>
>> llCarry = llAdd AND aCombo [X, nMask]
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> aCombo [X, nMask] = (aCombo [X, nMask] OR llAdd) AND NOT (aCombo [X, nMask] AND llAdd)
>>
>>
>> llAdd = llCarry
>> ENDFOR
>> llContinue = NOT llCarry
>>ENDDO
>>
>>
>>
>>
>>
>> Enjoy!
>>
>> Jay