Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mathematics knundrum
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00780672
Message ID:
00781225
Views:
50
Marvin --

We'll be interested in how many days it takes to calculate this!

I've forgotten the formulas for combinations/permutations. I believe, in this case, it's 2 to the n where n is the number of your list. Perhaps Bruce could point you to the precise formulation.

If that's the case, you have 2^70 combinations to test or approximately 1.180591620E+21. If I have my "illions" straight, that's 1.8 sextillion combinations to test. So, at 3 million, you've a ways to go.

If this is a one shot deal, you can just let it run, perhaps optimizing the loop (though it's pretty clean). It will end when it's done.

Alternatives:

1. If you could code this in C or assembly language, it would run several order of magnitudes faster.

2. There may be "smart" ways of eliminating combinations to test. However, those would tend to be data dependant on their efficacy and might not yield any real benefit.

3. There are some "hill-climbing" AI algorithms or genetic algorithms which might assist in finding one or approximate matches which would perform more quickly.

Best wishes!

Jay

>Jay, Most of the discussion is frankly above my head. I've run the program on several small datasets with perfect results in just seconds. Now I'm running it on a real dataset. I've created a test target that I know is valid. My real dataset has 70 numbers. The time to calculate has increased exponentially as it should. the program is running right now and already I'm up to 3,000,000 iterations of the main loop. I'm no statistician but I think something has gone arwy. I'll let it keep running for the fun of it. Thanks for all of your help...Marvin
>
>
>>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
>>>>		&&	If the current value is TRUE (a 1) and we're adding, set llCarry to .T.
>>>>		llCarry = llAdd AND aCombo [X, nMask]
>>>>
>>>>		&&  Assign the current value using an XOR function (not in VFP for logicals)
>>>>		&&	Truth table:
>>>>		&&	aCombo	llAdd	aCombo next state
>>>>		&&	.F.		.F.		.F.
>>>>		&&	.F.		.T.		.T.
>>>>		&&	.T.		.F.		.T.
>>>>		&&	.T.		.T.		.F.		value is carried to next digit
>>>>		
>>>>		aCombo [X, nMask] = (aCombo [X, nMask] OR llAdd) AND NOT (aCombo [X, nMask] AND llAdd)
>>>>		
>>>>		&&	Setup for next iteration of loop
>>>>		llAdd = llCarry
>>>>	ENDFOR
>>>>	llContinue = NOT llCarry		&&	If we're carrying on the last digit, we're done
>>>>ENDDO
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> Enjoy!
>>>>
>>>> Jay
Previous
Reply
Map
View

Click here to load this message in the networking platform