Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Mathematical Challenge (Lottery)
Message
De
01/06/2003 16:05:59
Walter Meester
HoogkarspelPays-Bas
 
 
À
01/06/2003 06:20:59
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00785796
Message ID:
00795012
Vues:
27
Hi gregory,

While you´ve taken the freedom to modify the code, the modifications do have a performance effect. The code below should be at least 50% faster
FUNCTION CheckForSumIsResult
PARAMETERS aNumbers, nNumbers, nResultNumber, nTotal, nIndex, cCalc

= CheckForSumIsResult2(nTotal, nIndex, cCalc)
RETURN


FUNCTION CheckForSumIsResult2(nTotal, nIndex, cCalc)
LOCAL nT

FOR nT = nIndex+1 TO nNumbers
	IF nTotal+aNumbers(nT) < nResultNumber 
		= CheckForSumIsResult2(nTotal+aNumbers(nT), nT, cCalc+"+"+ALLTRIM(STR(aNumbers[NT],10,2)))
	ELSE
		IF nTotal+aNumbers(nT) = nResultNumber
			? SUBSTR(cCalc+"+"+ALLTRIM(STR(aNumbers[nT],10,2)),2)
		ELSE
			** 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
		ENDIF
	ENDIF
ENDFOR
RETURN
>Reason: The algorithm I provided is designed to find a match where the emphasis is on providing a solution fast.
>I have a customer who has a ledger account with over 15000 transactions. Any amount booked (say 100 ) must be matched eventually with -100, or any combination that results in -100( eg 250, -50, -200, -30, -70). In these circumstances I am interested in a fast solution.

The difference is that my algorithm searches trough all possible answers and therefore does look for a subsequent answers. If in case 1, my algorith was modified, so it did stop if the answer was found, my algoritm would finish almost instantly.

You could easely modify the algorithm to search differently through the numbers so you could customize your searching path to increase the chance of finding a match faster.

Walter,
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform