Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mathematics knundrum
Message
From
14/05/2003 08:09:49
Walter Meester
HoogkarspelNetherlands
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00780672
Message ID:
00788120
Views:
31
>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

I only saw your question today, I hope you don't mind I respond. The solution I came up with is:
CREATE CURSOR Addup(Value B(2))

INSERT INTO addup VALUES (201.10)
INSERT INTO addup VALUES (25.18)
INSERT INTO addup VALUES (150.11)
INSERT INTO addup VALUES (778.88)
INSERT INTO addup VALUES (151.21)
INSERT INTO addup VALUES (11.31)
INSERT INTO addup VALUES (10.00)

nNumbers = RECCOUNT()
nDecimal = 2

** Sort them in ascending order to ensure that any number is lager than the previous number.
SELECT * FROM addup ORDER BY 1 INTO ARRAY aNumbers 

nResultNumber = 361.21

=CheckForSumIsResult(0,0,"")
RETURN

*-

FUNCTION CheckForSumIsResult(nTotal, nIndex, cCalc)
LOCAL nT

FOR nT = nIndex+1 TO nNumbers
	DO CASE
		CASE nTotal+aNumbers(nT) < nResultNumber
		 	=CheckForSumIsResult(nTotal+aNumbers(nT), nT, cCalc+"+"+ALLTRIM(STR(aNumbers[NT],10,nDecimal)))
	
		CASE nTotal+aNumbers(nT) = nResultNumber
			? SUBSTR(cCalc+"+"+ALLTRIM(STR(aNumbers[nT],10,nDecimal)),2)
			
		OTHERWISE
			** The sum is larger than the resultnumber
			** There is no need to search further as all remaining numbers are even larger
			** So exit here
			EXIT
	ENDCASE
ENDFOR
RETURN
HTH,

Walter,
Previous
Reply
Map
View

Click here to load this message in the networking platform